Android AlertDialog的几种用法介绍

发布时间:2021-08-17 18:34:11 作者:chen
来源:亿速云 阅读:124

这篇文章主要介绍“Android AlertDialog的几种用法介绍”,在日常操作中,相信很多人在Android AlertDialog的几种用法介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android AlertDialog的几种用法介绍”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

AlertDialog的几种用法

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="简单的dialog"
        android:onClick="dialog_1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表的dialog"
        android:onClick="dialog_2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选的dialog"
        android:onClick="dialog_3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选的dialog"
        android:onClick="dialog_4"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义View的dialog"
        android:onClick="dialog_5"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用adapter的dialog"
        android:onClick="dialog_6"/>
</LinearLayout>

java代码:

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<String> adapter = new ArrayAdapter<String>(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();
    }
}

 Android AlertDialog的几种用法介绍

Android AlertDialog的几种用法介绍

Android AlertDialog的几种用法介绍

Android AlertDialog的几种用法介绍

Android AlertDialog的几种用法介绍

Android AlertDialog的几种用法介绍

到此,关于“Android AlertDialog的几种用法介绍”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Android基础View回顾
  2. Android如何实现弹出列表、单选、多选框

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android alertdialog

上一篇:Linux系统下怎么进行大文件的切割和合并

下一篇:怎么在JavaScript中以不同的方式声明变量

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》