在Android应用中,遇到多次点击按钮导致的重复提交订单的问题。该问题有两种解决方案,一是后台对近似时间的订单进行过滤,二则在应用层面避免重复点击按钮。
这里,我们通过handler+TimerTask计时器结合实现屏蔽按钮2秒的功能:
View Code
1 TimerTask task ; 2 Timer timer; 3 4 timer = new Timer(true); 5 6 soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100); 7 loadId = soundPool.load(context, R.raw.zero, 1); 8 loadId1 = soundPool.load(context, R.raw.one, 2); 9 loadId2 = soundPool.load(context, R.raw.two, 3); 10 loadId3 = soundPool.load(context, R.raw.three, 4); 11 loadId4 = soundPool.load(context, R.raw.four, 5); 12 loadId5 = soundPool.load(context, R.raw.five, 4); 13 loadId6 = soundPool.load(context, R.raw.six, 3); 14 loadId7 = soundPool.load(context, R.raw.seven, 2); 15 loadId8 = soundPool.load(context, R.raw.eight, 1); 16 loadId9 = soundPool.load(context, R.raw.nine, 1); 17 18 } 19 20 21 Handler buttonhandler = new Handler(){ 22 public void handleMessage(Message msg) { 23 switch (msg.what) { 24 case 1: 25 nextbutton.setEnabled(true); 26 button1.setEnabled(true); 27 button2.setEnabled(true); 28 button3.setEnabled(true); 29 button4.setEnabled(true); 30 button5.setEnabled(true); 31 button6.setEnabled(true); 32 break; 33 } 34 super.handleMessage(msg); 35 } 36 }; 37 38 nextbutton.setEnabled(false); 39 buttonhide(nextbutton); 40 41 private void buttonhide(Button b){ 42 b.setEnabled(false); 43 44 if (task != null){ 45 task.cancel(); //将原任务从队列中移除 \ 46 } 47 task = new TimerTask(){ 48 public void run() { 49 Message message = new Message(); 50 message.what = 1; 51 buttonhandler.sendMessage(message); 52 } 53 }; 54 timer.schedule(task,2000); //延时1000ms后执行,1000ms执行一次 55 }
http://blog.csdn.net/andych008/article/details/7802032
http://www.cnblogs.com/xzf158/archive/2009/09/04/1560042.html