Android Double click 实现

android中提供的事件,仅有单击即click,未有double click event,但有些时候是需要double click来解决一些问题的……

若是button,可比较一下两次单击的时间间隔,下述以两次间隔小于500ms,认定其为DoubleClick

private long lastClickTime=0;public void btnMfClick(View view) {    if (Math.abs(lastClickTime-System.currentTimeMillis())<500){        Toast.makeText(this,"double click",Toast.LENGTH_SHORT).show();        lastClickTime=0;    }else {        lastClickTime=System.currentTimeMillis();        Toast.makeText(this,"Once click",Toast.LENGTH_SHORT).show();    }}

若在listview中,item的double click不仅需要同button一样比较当前click 时间与最后一次click的时间间隔,还需要确认一下当前click的item是不是上一次click的item……

//最后一次单击itemID,时间
private String intLastClickItem="";
private long lastClickTime;

/**lvTicket添加item click侦听器 * double click,则将click项从items中移除*/lvTicket.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {      String strPos=((Integer)position).toString();      if (intLastClickItem.equals(strPos)&&(Math.abs(lastClickTime-System.currentTimeMillis())<1000)){          lastClickTime=0;//double click后置0          intLastClickItem ="";//double click后置空          partialAdp.removeData(position);          lvTicket.setAdapter(partialAdp);          tvTransferCount.setText(strPos);      }else {          intLastClickItem =strPos;//click时记录item postion          lastClickTime=System.currentTimeMillis();//click时记录 当时时间      }    }});
(0)

相关推荐