欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中AlertDialog四种对话框的最科学编写用法(实例代码)

程序员文章站 2022-05-31 12:44:10
首先我们上图:  xml的代码如下,用于编写按钮: ...

首先我们上图:

Android中AlertDialog四种对话框的最科学编写用法(实例代码)

 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:background="@drawable/background"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
  <button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="简单的dialog"
    />
  <button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="列表的dialog"
    />
  <button
    android:id="@+id/button_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="单选的dialog"
    />
  <button
    android:id="@+id/button_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="多选的dialog"
    />
</linearlayout>

java代码如下,用于实现逻辑:

import androidx.appcompat.app.actionbar;
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.button;
import android.widget.toast;
public class mainactivity extends appcompatactivity{
  int index;
  string [] item = {"android","ios","spark","hadoop","web"};
  boolean[] bools = {false,false,false,false,false};
  // 设置boolean数组所有的选项设置默认没选
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    actionbar actionbar = getsupportactionbar();
    if (actionbar != null) {
      actionbar.hide();
    }
    button button=(button)findviewbyid(r.id.button_1);
    button.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
        builder.seticon(r.drawable.girl);
        builder.settitle("标题栏");
        builder.setmessage("对话框内容,可自行设置");
        builder.setpositivebutton("确定",new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
            toast.maketext(mainactivity.this, "点击了确定", toast.length_short).show();
          }
        });
        builder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialoginterface, int i) {
            toast.maketext(mainactivity.this, "点击了取消", toast.length_short).show();
          }
        });
        builder.setneutralbutton("好的", new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialoginterface, int i) {
            toast.maketext(mainactivity.this, "点击了“好的”", toast.length_short).show();
          }
        });
        alertdialog alertdialog = builder.create();
        alertdialog.show();
      }
    });
    button button2=(button)findviewbyid(r.id.button_2);
    button2.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
        builder.settitle("请选择一个技术分支");
        builder.setitems(item, new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
            toast.maketext(mainactivity.this, "选择了"+item[which], toast.length_short).show();
          }
        });
        // 取消可以不添加
        //builder.setnegativebutton("取消",null);
        alertdialog alertdialog = builder.create();
        alertdialog.show();
      }
    });
    button button3=(button)findviewbyid(r.id.button_3);
    button3.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
        builder.settitle("请选择技术分支:");
        builder.setsinglechoiceitems(item, index, new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
            index = which;
          }
        });
        builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
            toast.maketext(mainactivity.this, "选择了"+item[index], toast.length_short).show();
          }
        });
        builder.setnegativebutton("取消",null);
        alertdialog alertdialog = builder.create();
        alertdialog.show();
      }
    });
    button button4=(button)findviewbyid(r.id.button_4);
    button4.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
        builder.settitle("请选择技术分支:");
        builder.setmultichoiceitems(item, bools, new dialoginterface.onmultichoiceclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which, boolean ischecked) {
            bools[which] = ischecked;
          }
        });
        builder.setpositivebutton("确定", new dialoginterface.onclicklistener() {
          @override
          public void onclick(dialoginterface dialog, int which) {
            stringbuffer sb = new stringbuffer();
            for (int i = 0; i < item.length; i++) {
              if (bools[i]) {
                sb.append(item[i] + " ");
              }
            }
            toast.maketext(mainactivity.this, "选择了" + sb.tostring(), toast.length_short).show();
          }
        });
        builder.setnegativebutton("取消",null);
        alertdialog alertdialog = builder.create();
        alertdialog.show();
      }
    });
  }
}

总结

以上所述是小编给大家介绍的android中alertdialog四种对话框的最科学编写用法,希望对大家有所帮助