3.26

所花时间(包括上课):1.5

打码量(行):300

博客量(篇):1

了解到知识点:学习提醒对话框AlertDialog

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AlertDialog;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Button showAlertButton = findViewById(R.id.btn_show_alert);

        showAlertButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 创建 AlertDialog.Builder 对象

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

 

                // 设置对话框标题和消息内容

                builder.setTitle("Alert")

                       .setMessage("This is an AlertDialog example.");

 

                // 设置PositiveButton按钮及其点击事件

                builder.setPositiveButton("OK", (dialog, which) -> {

                    // 点击“确定”按钮后的操作

                    // 可以在这里添加处理逻辑

                    dialog.dismiss(); // 关闭对话框

                });

 

                // 设置NegativeButton按钮及其点击事件

                builder.setNegativeButton("Cancel", (dialog, which) -> {

                    // 点击“取消”按钮后的操作

                    // 可以在这里添加处理逻辑

                    dialog.dismiss(); // 关闭对话框

                });

 

                // 创建并显示AlertDialog

                AlertDialog alertDialog = builder.create();

                alertDialog.show();

            }

        });

    }

}

<!-- activity_main.xml -->

<Button

    android:id="@+id/btn_show_alert"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Show Alert Dialog" />

 

posted @ 2024-03-26 22:05  赵千万  阅读(8)  评论(0编辑  收藏  举报