1.定义弹出的对话框的按钮和显示结果的文本框 <Button android:id="@+id/btn7" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_nine_selector" android:text="弹出对话提醒"> </Button> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content"> </TextView>
2.代码
private TextView t1;
Buton btn7 = findViewById(R.id.btn7); btn7.setOnClickListener(this::onClick); t1 = findViewById(R.id.text2);
private void onClick(View view) { Log.d("点击数据", "onClick: "); //创建提醒对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置对话框标题 builder.setTitle("尊敬的用户"); //设置文本内容 builder.setMessage("你要卸载我妈?"); //设置肯定按钮监听 builder.setPositiveButton("残忍卸载", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { t1.setText("确定卸载!"); } }); //设置否定按钮监听 builder.setNegativeButton("我再想想", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { t1.setText("继续使用!"); } }); //构建对话框 AlertDialog dailog = builder.create(); //显示对话框 dailog.show(); }