Android 简单模拟鼠标双击退出应用程序,利用Toast提示“再点一次退出”
使用Toast
// 参数:当前上下文环境,提示文本,显示时长,最后利用show()将提示信息显示在UI
Toast.makeText(context,text,duration).show();
鼠标单击提示信息
XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单击出现提示信息"/>
</LinearLayout>
MainActivity文件
package com.example.my_toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn_click);
//添加监听事件,当点击按钮时,提示“单击一次鼠标左键”
button.setOnClickListener((view) - > {
Toast.makeText(this,"单击一次鼠标左键",Toast.LENGTH_SHORT).show();
});
}
}
鼠标双击实现退出系统
实现原理:设置监听事件,当第一次单击鼠标时,显示提示信息并且调用“鼠标双击监听”函数,监听第二次点击按钮的时间与第一次的时间间隔是否小于两秒,小于则退出应用程序,否则将第二次点击鼠标的时间当成上一次点击鼠标的时间,用于下一次判断,同时提示Toast信息
XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_marginLeft="95dp"
android:id="@+id/button_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
<Button
android:id="@+id/button_btn"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
</LinearLayout>
</LinearLayout>
布局文件效果图:
MainActivity文件
package com.example.my_toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static long lastTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button_btn);
Button btn1 = findViewById(R.id.button_btn1);
EditText username = findViewById(R.id.edit_username);
EditText password = findViewById(R.id.edit_password);
btn1.setOnClickListener(view -> {
if(!(username.equals("20200") && password.equals("20200"))) {
Toast.makeText(this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(view -> {
//第一次点击,发出提示,并且调用OnBackPressed方法进行今天第二次点击
Toast.makeText(this, "再点一次退出系统", Toast.LENGTH_SHORT).show();
OnBackPressed();
});
}
public void OnBackPressed() {
boolean flag = false;
if (lastTime == 0) {
// 第一次点击时,记录第一次点击的时间
lastTime = new Date().getTime();
} else {
// 第二次点击时,记录第二次点击的时间
long secondTime = new Date().getTime();
// 两次点击时间间隔不能大于2s
if (secondTime - lastTime <= 2000) {
finish();
} else {
lastTime = secondTime;
Toast.makeText(this, "再点一次退出系统", Toast.LENGTH_SHORT).show();
}
}
}
}