main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >     <ToggleButton        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textOn="ON"        android:textOff="OFF"        android:text="短震动" />     <ToggleButton        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textOn="ON"        android:textOff="OFF"        android:text="长震动" />     <ToggleButton        android:id="@+id/btn3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textOn="ON"        android:textOff="OFF"        android:text="节奏震动" /> </LinearLayout>

Java代码如下

package org.lxh.demo; 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 Hello extends Activity {private ToggleButton btn1 = null;private ToggleButton btn2 = null;private ToggleButton btn3 = null;private Vibrator myVibrator = null; public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); // 生命周期方法super.setContentView(R.layout.main); // 设置要使用的布局管理器this.myVibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);this.btn1 = (ToggleButton) super.findViewById(R.id.btn1);this.btn2 = (ToggleButton) super.findViewById(R.id.btn2);this.btn3 = (ToggleButton) super.findViewById(R.id.btn3);this.btn1.setOnClickListener(new Btn1());this.btn2.setOnClickListener(new Btn2());this.btn3.setOnClickListener(new Btn3()); } private class Btn1 implements OnClickListener { public void onClick(View arg0) {if (btn1.isChecked()) {Hello.this.myVibrator.vibrate(new long[] { 100, 10, 100, 1000 }, -1);Toast.makeText(Hello.this, "短震动", Toast.LENGTH_SHORT).show();} else {Hello.this.myVibrator.cancel();Toast.makeText(Hello.this, "取消短震动", Toast.LENGTH_SHORT).show();} } } private class Btn2 implements OnClickListener { public void onClick(View arg0) {if (btn2.isChecked()) {Hello.this.myVibrator.vibrate(new long[] { 100, 100, 100, 1000 }, 0);Toast.makeText(Hello.this, "长震动", Toast.LENGTH_SHORT).show();} else {Hello.this.myVibrator.cancel();Toast.makeText(Hello.this, "取消长震动", Toast.LENGTH_SHORT).show();} } } private class Btn3 implements OnClickListener { public void onClick(View arg0) {if (btn3.isChecked()) {Hello.this.myVibrator.vibrate(new long[] { 1000, 50, 1000, 50 }, 0);Toast.makeText(Hello.this, "节奏震动", Toast.LENGTH_SHORT).show();} else {Hello.this.myVibrator.cancel();Toast.makeText(Hello.this, "取消节奏震动", Toast.LENGTH_SHORT).show();} } }}
震动权限: android.permission.VIBRATE

更多相关文章

  1. 自学android——AutoCompleteTextView的使用
  2. Android中两个控件滑动效果冲突的解决办法
  3. Android-线性布局的经典案例1-计算器
  4. Android与Javascript交互示例(一)
  5. Android自学笔记(番外篇):全面搭建Linux环境(二)——VMware Workstati
  6. Android日期对话框
  7. Android(安卓)完全退出
  8. Android下修改SeekBar样式
  9. 3. android 自动完成文本框

随机推荐

  1. 【Android】如何让跑马灯跑起来-控件请求
  2. Android根据文件路径使用File类获取文件
  3. 安卓xml文件中设置动画匀速旋转无效?
  4. 系出名门Android(4) - 活动(Activity),
  5. android xml解析
  6. 【边做项目边学Android】小白会遇到的问
  7. Android(安卓)blueZ HCI(一个):hciconfig实
  8. Android之Handler用法总结
  9. 【视频课程】Android底层开发关键技术—A
  10. 详解Android中AsyncTask的使用