package irdc.zhendong; import irdc.zhendong.R; import android.app.Activity; import android.app.Service; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import android.widget.ToggleButton; public class zhendong extends Activity { private Vibrator mVibrator01; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*设置ToggleButton的对象*/ mVibrator01 = ( Vibrator )getApplication().getSystemService (Service.VIBRATOR_SERVICE); final ToggleButton mtogglebutton1 = (ToggleButton) findViewById(R.id.myTogglebutton1); final ToggleButton mtogglebutton2 = (ToggleButton) findViewById(R.id.myTogglebutton2); final ToggleButton mtogglebutton3 = (ToggleButton) findViewById(R.id.myTogglebutton3); /* 短震动 */ mtogglebutton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton1.isChecked()) { /* 设置震动的周期 */ mVibrator01.vibrate( new long[]{100,10,100,1000},-1); /*用Toast显示震动启动*/ Toast.makeText ( zhendong.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /*用Toast显示震动已被取消*/ Toast.makeText ( zhendong.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } }); /* 长震动 */ mtogglebutton2.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton2.isChecked()) { /*设置震动的周期*/ mVibrator01.vibrate(new long[]{100,100,100,1000},0); /*用Toast显示震动启动*/ Toast.makeText ( zhendong.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /* 用Toast显示震动取消 */ Toast.makeText ( zhendong.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } }); /* 节奏震动 */ mtogglebutton3.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton3.isChecked()) { /* 设置震动的周期 */ mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0); /*用Toast显示震动启动*/ Toast.makeText ( zhendong.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /* 用Toast显示震动取消 */ Toast.makeText ( zhendong.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } }); } }
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 建立第一個TextView --> <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <!-- 建立第二個TextView --> <TextView android:id="@+id/myTextView2" android:layout_width="127px" android:layout_height="35px" android:layout_x="90px" android:layout_y="33px" android:text="@string/str_text1" /> <!-- 建立第三個TextView --> <TextView android:id="@+id/myTextView3" android:layout_width="127px" android:layout_height="35px" android:layout_x="90px" android:layout_y="115px" android:text="@string/str_text2" /> <!-- 建立第四個TextView --> <TextView android:id="@+id/myTextView4" android:layout_width="127px" android:layout_height="35px" android:layout_x="90px" android:layout_y="216px" android:text="@string/str_text3" /> <!-- 建立第一個ToggleButton --> <ToggleButton android:id="@+id/myTogglebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="29px" android:layout_y="31px" /> <!-- 建立第二個ToggleButton --> <ToggleButton android:id="@+id/myTogglebutton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="29px" android:layout_y="114px" /> <!-- 建立第三個ToggleButton --> <ToggleButton android:id="@+id/myTogglebutton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="29px" android:layout_y="214px" /> </AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.0" package="irdc.zhendong"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name="irdc.zhendong.zhendong"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE" /> </manifest>
时间: 2024-11-05 14:51:04