标签:
1.之前我编写的代码是如下:
1 package com.himi.vibrate; 2 3 import android.app.Activity; 4 import android.app.Service; 5 import android.content.res.AssetManager; 6 import android.graphics.Typeface; 7 import android.os.Bundle; 8 import android.os.Vibrator; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 13 public class MainActivity extends Activity implements OnClickListener { 14 private Button btnLow; 15 private Button btnNormal; 16 private Button btnFast; 17 18 Vibrator vibrator; 19 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE); 26 27 btnLow = (Button) findViewById(R.id.btnlow); 28 btnNormal = (Button) findViewById(R.id.btnnormal); 29 btnFast = (Button) findViewById(R.id.btnfast); 30 //下面这五行代码是实现button上面的文字显示楷体,不用理会 31 32 AssetManager mgr = getAssets(); 33 Typeface tf = Typeface.createFromAsset(mgr, "fonts/DroidSansFallback.ttf"); 34 35 btnLow.setTypeface(tf); 36 btnNormal.setTypeface(tf); 37 btnFast.setTypeface(tf); 38 39 } 40 41 @Override 42 public void onClick(View v) { 43 if (v.equals(btnLow)) { 44 vibrator.vibrate(new long[] {1000,30,100,1000},0); 45 } 46 if (v.equals(btnNormal)) { 47 48 vibrator.vibrate(new long[] {800,40,400,30},0); 49 } 50 if (v.equals(btnFast)) { 51 vibrator.vibrate(new long[] {30,100,20,50},0); 52 } 53 54 } 55 56 protected void onStop() { 57 if(null!=vibrator){ 58 vibrator.cancel(); 59 } 60 super.onStop(); 61 } 62 }
2.后来才知道原来问题是如此简单,修改完的代码如下:
1 package com.himi.vibrate; 2 3 import android.app.Activity; 4 import android.app.Service; 5 import android.content.res.AssetManager; 6 import android.graphics.Typeface; 7 import android.os.Bundle; 8 import android.os.Vibrator; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 13 public class MainActivity extends Activity implements OnClickListener { 14 private Button btnLow; 15 private Button btnNormal; 16 private Button btnFast; 17 18 Vibrator vibrator; 19 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE); 26 27 btnLow = (Button) findViewById(R.id.btnlow); 28 btnNormal = (Button) findViewById(R.id.btnnormal); 29 btnFast = (Button) findViewById(R.id.btnfast); 30 31 AssetManager mgr = getAssets(); 32 Typeface tf = Typeface.createFromAsset(mgr, "fonts/DroidSansFallback.ttf"); 33 34 btnLow.setTypeface(tf); 35 btnNormal.setTypeface(tf); 36 btnFast.setTypeface(tf); 37 38 //之前没有加这3条语句,振动服务是没有响应的。后来发现这是语法没有弄清楚,我就是个傻逼 39 btnFast.setOnClickListener(this);//监听当前MainActivity 40 btnNormal.setOnClickListener(this);//监听当前MainActivity 41 btnLow.setOnClickListener(this);//监听当前MainAcitivity 42 43 } 44 45 @Override 46 public void onClick(View v) { 47 if (v.equals(btnLow)) { 48 vibrator.vibrate(new long[] {1000,30,100,1000},0); 49 } 50 if (v.equals(btnNormal)) { 51 52 vibrator.vibrate(new long[] {800,40,400,30},0); 53 } 54 if (v.equals(btnFast)) { 55 vibrator.vibrate(new long[] {30,100,20,50},0); 56 } 57 58 } 59 60 61 protected void onStop() { 62 if(null!=vibrator){ 63 vibrator.cancel(); 64 } 65 super.onStop(); 66 } 67 }
3.总结
这里是MainAcitivity实现了OnClickListener接口,所以接口监听功能是属于当前MainActivity的,所以是要监听当前MainActivity的:
语法格式如下:
public class MainActivity extends Activity implements OnClickListener {
………………
public void onCreate(Bundle savedInstanceState) {
…………
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);//监听当前Acitivity,当然这里监听的就是MainActivity
}
public void onClick(View v) {
if (v.equals(btn1)) {
…………
}
……………………
}
Android(java)学习笔记113:Android编写代码调用Vibrator震动功能,按下按钮button始终没有震动
标签:
原文地址:http://www.cnblogs.com/hebao0514/p/4590188.html