Vibrator结合ToggleButton控件的震动提示

添加权限:

 

<uses-permission
		android:name="android.permission.VIBRATE" />

 

	private Vibrator mVibrator01;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vibrator);

		mVibrator01 = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
		final ToggleButton mtogglebutton1 = (ToggleButton) findViewById(R.id.toggleButton1);

		mtogglebutton1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (mtogglebutton1.isChecked()) {
					/* 设定震动的周期 */
					mVibrator01.vibrate(new long[] { 100, 10, 100, 1000 }, -1);
					/* 用Toast显示震动启动 */
					Toast.makeText(MyVibrator.this, "震动执行中",
							Toast.LENGTH_SHORT).show();
				} else {
					/* 取消震动 */mVibrator01.cancel();
					/* 用Toast显示震动取消 */
					Toast.makeText(MyVibrator.this, "震动已取消",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

 

 

Vibrator结合ToggleButton控件的震动提示
 
Vibrator结合ToggleButton控件的震动提示

 

 

你可能感兴趣的:(button)