Android点击按钮退出程序并提醒
2024-09-04 20:49:53
效果展示:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { Button exitButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); exitButton = (Button)findViewById(R.id.exit);
exitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("退出程序")
.setMessage("是否退出程序")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
return;
}
}).create();
alertDialog.show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"> <Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
最新文章
- C# Winfrom 页面传值
- MYSQL的增删改查语句样码
- UNION 查询中的排序
- Qt之QHostInfo
- SVN四部曲之SVN命令精通
- 新发现:原来java正则表达式不写^和$也可以运行
- linux下fdisk分区管理、文件系统管理、挂载文件系统等
- 如何自动增加和从代码读取Xcode项目的版本号
- Linux编程 10 (shell外部命令与内建命令,alias ,type命令)
- SQL SERVER孤立帐号的处理
- 【Beta Scrum】冲刺!4/5
- window 10系统怎样手动更改电脑的时间
- 洛谷 P3994 高速公路
- Python学习---IO的异步[自定义异步IO]
- Flask中的before_request装饰器和after_request装饰器以及WTForms组件
- Android序列化:Parcelable/Serializable
- 20155207 2006-2007-2 《Java程序设计》第3周学习总结
- mysql数据库批量操作
- 使用JS完成注册表单的数据校验
- 易语言调用csharp写的COM组件的程序在Win2008上奔溃的解决办法