安卓双击事件

1、

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:onClick="click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="双击事件" />

</RelativeLayout>

2、

public class MainActivity extends Activity {
long firstClickTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
if(firstClickTime>0){
long sencondClickTime = SystemClock.uptimeMillis();
long dtime = sencondClickTime - firstClickTime;
if(dtime<500){
Toast.makeText(this, "双击了", 0).show();
}else{
firstClickTime = 0;
}
return;
}
firstClickTime = SystemClock.uptimeMillis();
new Thread(){
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
firstClickTime = 0;
};
}.start();
}
}

发布了56 篇原创文章 · 获赞 12 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/huwan12345/article/details/63249588