<AlertDialog>的简单用法与功能
来源:互联网 发布:新网互联域名证书 编辑:程序博客网 时间:2024/04/30 15:57
package com.crazyit.ui.dialogdemo;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.LayoutInflater;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;/** * Dialog * AlertDialog 是父类 * 3个子类 ProgressDialog DataPickerDialog TimePickerDialog * <p/> * AlertDialog 生成的对话框分为以下4个区域: * 图标区 标题区 内容区 按钮区 */public class MainActivity extends AppCompatActivity { TextView show; Button btn, btn2,btn3,btn4,btn5,btn6; String[] str = new String[]{"连城诀","雪山飞狐", "神雕侠侣", "天龙八部", "射雕英雄传", "倚天屠龙记"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (TextView) findViewById(R.id.show); btn = (Button) findViewById(R.id.button); btn2 = (Button) findViewById(R.id.button2); btn3 = (Button) findViewById(R.id.button3); btn4 = (Button) findViewById(R.id.button4); btn5 = (Button) findViewById(R.id.button5); btn6 = (Button) findViewById(R.id.button6); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单对话框") //设置对话框标题 .setIcon(R.mipmap.fuqi) //设置图标 .setMessage("对话框的测试内容\n 第二行内容"); //设置内容 //添加确定按钮 setPositiveButton(builder); setNegativeButton(builder) //添加取消按钮 .create().show(); } }); //简单列表项的对话框 btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单列表项的对话框") .setIcon(R.mipmap.fuqi) .setItems(str, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你选中了 <<" + str[which] + ">>"); } }); setPositiveButton(builder); setNegativeButton(builder) //添加取消按钮 .create().show(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("单选列表项对话框") .setIcon(R.mipmap.fuqi) // 数字 1 代表是默认选中 索引为1的列表项 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你选中了 <<" + str[which] + ">>"); dialog.dismiss(); } });//注释 表示去掉 确定和取消按钮// setPositiveButton(builder);// setNegativeButton(builder) //添加取消按钮 builder.create().show(); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("多选列表项对话框") .setIcon(R.mipmap.fuqi) .setMultiChoiceItems(str, new boolean[]{false, true, true, false, true, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { show.setText("你选中了 <<" + str[which] + ">>"); } }); setPositiveButton(builder); setNegativeButton(builder) //添加取消按钮 .create().show(); } }); btn5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("自定义列表项") .setIcon(R.mipmap.fuqi) .setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.array_item, str), null); setPositiveButton(builder); setNegativeButton(builder) //添加取消按钮 .create().show(); } }); btn6.setOnClickListener(new View.OnClickListener() { //加载布局界面 // LinearLayout login = (LinearLayout) getLayoutInflater().inflate(R.layout.login,null); LayoutInflater inflater = LayoutInflater.from(MainActivity.this); LinearLayout login = (LinearLayout) inflater.inflate(R.layout.login, null, false); @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("自定义View对话框") .setIcon(R.mipmap.fuqi) .setView(login) //设置 对话框显示的自定义view .setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create().show(); } }); } private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) { return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了[取消]按钮"); } }); } private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) { //调用添加 确定按钮 return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了[确定]按钮!"); } }); }}
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.crazyit.ui.dialogdemo.MainActivity"> <!-- 显示一个普通的文本编辑框组件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示提示消息的对话框" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="简单列表项对话框" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选列表项对话框" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选列表项对话框" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义列表项对话框" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义View对话框" /> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" /></LinearLayout>
AlertDialog 提供了6种方法来指定对话框的内容.
- setMessage() ;设置对话框内容为简单文本内容
- setItems( );设置对话框内容为简单的列表项
- setSingleChoiceItems();设置对话框内容为单选列表项
- setMultiChoiceItems();设置对话框内容为多选列表项
- setAdapter(); 设置对话框内容为自定义列表项
- setView() 设置对话框内容为自定义 view
0 0
- <AlertDialog>的简单用法与功能
- AlertDialog 的简单用法效果图
- AlertDialog的超简单用法
- ProgressBar 的功能与 简单用法
- <ViewSwitcher>的简单功能与用法
- 第33章:AlertDialog对话框的功能和用法
- android学习总结(16.09.08)AlertDialog的简单用法
- Android--AlertDialog的用法
- AlertDialog的各种用法
- AlertDialog的用法
- AlertDialog的用法
- AlertDialog的用法
- AlertDialog的用法
- Textswitcher的简单用法与功能(文本切换器)
- <SeekBar>拖动条的 简单功能与用法
- <SeekBar>拖动条的 简单功能与用法
- AlertDialog的简单实例
- AlertDialog的简单学习
- 欢迎使用CSDN-markdown编辑器
- JAVA中集合框架概述
- 【mybatis】IF判断的坑
- LINUX常用命令大全
- 笔记本安装WindowsXP系统时蓝屏了怎么办
- <AlertDialog>的简单用法与功能
- iOS多线程编程--NSOperation
- Nginx两种安装方式简述
- C++第七次上机实验
- 【视频】熊猫TV(原韩国orgtv)女主播崔智燕诱惑视频
- 解决jsp页面读取XXX.properties文件显示文乱码的问题
- 15.4 迭代器要注意的事项
- COGS 92.一元三次方程求解 解题报告
- 15.5 ArrayList的原理