效果展示:

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>

最新文章

  1. C# Winfrom 页面传值
  2. MYSQL的增删改查语句样码
  3. UNION 查询中的排序
  4. Qt之QHostInfo
  5. SVN四部曲之SVN命令精通
  6. 新发现:原来java正则表达式不写^和$也可以运行
  7. linux下fdisk分区管理、文件系统管理、挂载文件系统等
  8. 如何自动增加和从代码读取Xcode项目的版本号
  9. Linux编程 10 (shell外部命令与内建命令,alias ,type命令)
  10. SQL SERVER孤立帐号的处理
  11. 【Beta Scrum】冲刺!4/5
  12. window 10系统怎样手动更改电脑的时间
  13. 洛谷 P3994 高速公路
  14. Python学习---IO的异步[自定义异步IO]
  15. Flask中的before_request装饰器和after_request装饰器以及WTForms组件
  16. Android序列化:Parcelable/Serializable
  17. 20155207 2006-2007-2 《Java程序设计》第3周学习总结
  18. mysql数据库批量操作
  19. 使用JS完成注册表单的数据校验
  20. 易语言调用csharp写的COM组件的程序在Win2008上奔溃的解决办法

热门文章

  1. Python自动化 unittest生成测试报告(HTMLTestRunner)03
  2. 如何使用 VS Code 远程连接矩池云主机
  3. CF1428B题解
  4. Keras速查_CPU和GPU的mnist预测训练_模型导出_模型导入再预测_导出onnx并预测
  5. 前端经典面试题vue面试题
  6. 使用SymPy
  7. kubernetes如何强制删除namespace
  8. Django基础必会
  9. Docker安装与基本命令
  10. google hacker语法