最近在学习linux内核定时器,所以用类似定时器的方法实现按钮的双击事件
public class MainActivity extends Activity {

 private Handler handler = new Handler() {

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch (msg.what) {
   case 1:
    doSome1();
    break;
   case 2:
    dosome2();
    break;
   }
  }

 };

 private void doSome1() {
  // Toast.makeText(MainActivity.this, "测试111111111111111", Toast.LENGTH_SHORT).show();
  firstTime = 0;
 }

 private void dosome2() {
  Toast.makeText(MainActivity.this, "测试222222222", Toast.LENGTH_SHORT).show();

 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  findView();

 }

 long firstTime = 0;

 /**
  * 实例代控件
  */
 private void findView() {
  this.findViewById(R.id.btn_test).setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    choseState();
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 private void choseState() {

  // handler.sendEmptyMessage(1);
  if (firstTime == 0) {
   firstTime = System.currentTimeMillis();
   // 单击
   handler.sendEmptyMessageDelayed(1, 1000);
  } else {
   if (System.currentTimeMillis() - firstTime < 800) {
    // 双击
    handler.sendEmptyMessage(2);
   }
  }

 }
}

Logo

更多推荐