1.提醒对话框AlertDialog
1.1activity_alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出提醒对话框"/> <TextView android:id="@+id/tv_alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"/> </LinearLayout>
1.2AlertDialogActivity.java
package com.example.chapter05; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv_alert; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert_dialog); findViewById(R.id.btn_alert).setOnClickListener(this); tv_alert = findViewById(R.id.tv_alert); } @Override public void onClick(View v) { // 创建提醒对话框的建造器 AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置对话框的标题文本 builder.setTitle("尊敬的用户"); // 设置对话框的内容文本 builder.setMessage("你真的要卸载我吗?"); // 设置对话框的肯定按钮文本及其点击监听器 builder.setPositiveButton("残忍卸载", (dialog, which) -> { tv_alert.setText("虽然依依不舍,但是只能离开了"); }); // 设置对话框的否定按钮文本及其点击监听器 builder.setNegativeButton("我再想想", (dialog, which) -> { tv_alert.setText("让我再陪你三百六十五个日夜"); }); // 根据建造器构建提醒对话框对象 AlertDialog dialog = builder.create(); dialog.show(); } }
1.3效果:
标签:10,dialog,对话框,tv,builder,23,alert,2022,import From: https://www.cnblogs.com/pingfanliliang/p/16819692.html