iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android AlertDialog的几种用法详解
  • 640
分享到

Android AlertDialog的几种用法详解

2024-04-02 19:04:59 640人浏览 薄情痞子
摘要

AlertDialog的几种用法 xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLa

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方法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android AlertDialog的几种用法详解

本文链接: https://www.lsjlt.com/news/132806.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作