• 欢迎访问1024小神,一个只会Python的程序猿不是一个好司机
  • 有什么想对我说的可以在留言板里给我留言哦~
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏1024小神吧

AlertDialog的几种用法

Java 1024小神 2周前 (07-08) 37次浏览 2个评论
<!--?xml version="1.0" encoding="utf-8"?-->


    <button></button>
    <button></button>
    <button></button>
    <button></button>
    <button></button>
    <button></button>

package com.example.lesson7_3_id19_alertdialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void dialog_1(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);

        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("取消",null);
        builder.setNeutralButton("中立",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
    private String [] item = {"游戏","运动","电影","旅游","看书"};
    public void dialog_2(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(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();

    }
    int index;
    public void dialog_3(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(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();
    }
    // 设置boolean数组所有的选项设置默认没选
    boolean[] bools = {false,false,false,false,false};
    public void dialog_4(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(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();
    }
    public void dialog_5(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("连接wifi");
        final EditText et = new EditText(this);
        et.setHint("请输入密码");
        et.setSingleLine(true);
        builder.setView(et);
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String password = et.getText().toString();
                if (password.equals("123456")) {
                    Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
    public void dialog_6(View v){
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,item);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("使用适配器");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}


如有失效,请留言告知丨转载请注明原文链接:AlertDialog的几种用法
点赞 (0)

您必须 登录 才能发表评论!

(2)个小伙伴在吐槽
  1. 萝莉菲菲
    1024!签到时间:2021-07-12 15:07:32,每日打卡,生活更精彩哦~
    2021-07-12 15:07
  2. qingmingmeng
    博主为了让大家学变成也是煞费苦心呐,今日份打卡
    2021-07-12 09:00