Android AlertDialog的几种用法详解

作者:中国人醒来了 时间:2023-12-02 18:15:50 

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的几种用法详解

来源:https://www.cnblogs.com/it-tsz/p/11210991.html

标签:Android,AlertDialog
0
投稿

猜你喜欢

  • Android中activity从创建到显示的基本介绍

    2023-01-08 02:07:41
  • C#用Topshelf创建Windows服务的步骤分享

    2022-10-19 00:47:58
  • c#的dllimport使用方法详解

    2023-04-20 04:01:49
  • spring webflux自定义netty 参数解析

    2023-07-26 18:38:25
  • Android sdutio配置Zxing进行扫码功能的实现方法

    2023-12-12 15:40:13
  • IDEA使用SequenceDiagram插件绘制时序图的方法

    2023-07-03 11:20:07
  • Android中实现长按修改ListView对象的内容

    2022-04-13 15:09:08
  • C#实现按照指定长度在数字前补0方法小结

    2023-02-23 09:42:32
  • Android互联网访问图片并在客户端显示的方法

    2021-12-26 21:25:10
  • Android仿水波纹流量球进度条控制器

    2022-09-02 01:13:03
  • Android Studio实现音乐播放器的全过程(简单易上手)

    2023-07-10 08:02:43
  • 解决JTable排序问题的方法详解

    2023-02-07 08:53:42
  • Android简单实现一个颜色渐变的ProgressBar的方法

    2023-07-23 00:30:12
  • C# 字符串多行显示/文本换行以textbox为例讲解

    2022-02-02 06:35:28
  • 基于SpringBoot Mock单元测试详解

    2021-09-25 02:49:41
  • java实现MapReduce对文件进行切分的示例代码

    2023-10-07 21:46:59
  • C#类的创建与初始化实例解析

    2023-08-05 17:55:31
  • Android中Retrofit的简要介绍

    2022-07-19 08:42:49
  • Android仿qq分组管理的第三方库

    2023-08-27 05:31:21
  • Android动态修改ToolBar的Menu菜单示例

    2021-10-29 15:36:07
  • asp之家 软件编程 m.aspxhome.com