雙擊事件在Windows中經常使用,大家都已經用得非常習慣了.但是在Android中默認是沒有雙擊事件的(這個我覺得很奇怪).在我的足球即時比分應用中就用到了雙擊事件,我上網查過也在一些QQ群中問過一些Android開發人員,他們說這個只能自己來處理了,Android是沒有提供雙擊事件的.
以下是我足球即時比分中的代碼:
public class SenseSoccerScoreActivity extends Activity{ // 雙擊事件記錄最近一次點擊的ID private String lastClickId; // 雙擊事件記錄最近一次點擊的時間 private long lastClickTime; @Override public void onCreate(Bundle savedInstanceState) { Log.d("score", "SenseSoccerScoreActivity create ..."); super.onCreate(savedInstanceState); ConfigUtil.setConfig(this, KEY_UPDATE_TIME, 0L); // 更新本應用的Locale ScoreUtil.updateAppLocale(this); LeagueUtil.init(this); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); setTheme(android.R.style.Theme_NoTitleBar); initMatchViews() ; } /** * 初始化賽事列表的數據 */ private void initMatchViews() { // ------------------ playing listView -------------------- MatchAdapter playingAdapter = mm.getPlayingAdapter(); playingAdapter.sortPlayingBy(MatchDateComparator.getInstance()); playingListView.setAdapter(playingAdapter); playingListView.setOnItemLongClickListener(todayListLongClickListener); // 添加點擊事件,雙擊的判斷由mItemClickListenter完成 playingListView.setOnItemClickListener(mItemClickListenter); playingListView.setOnTouchListener(listViewOnTouchListener); playingListView.setFastScrollEnabled(true); } /** * 雙擊事件(賽事明細,事件) */ private OnItemClickListener mItemClickListenter = new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int pos,long id) { MatchVO mv = (MatchVO) ((ListView)parent).getAdapter().getItem(pos); // 如果是雙擊,1秒內連續點擊判斷為雙擊 if(mv.getId().equals(lastClickId) && (Math.abs(lastClickTime-System.currentTimeMillis()) < 1000)){ lastClickId = null; lastClickTime = 0; Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.putExtra("matchId", mv.getId()); intent.putExtra("matchKey", mv.getMatchKey()); intent.setClass(SenseSoccerScoreActivity.this, MatchEventActivity.class); startActivity(intent); }else{ lastClickId = mv.getId(); lastClickTime = System.currentTimeMillis(); } } }; }