zoukankan      html  css  js  c++  java
  • 对话框AlertDialog的基本类型与创建

     

    测试代码:

     布局:

    activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     tools:context="com.zzw.testalerdialog.MainActivity" >
     7 
     8     <Button
     9         android:id="@+id/horizontally"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:layout_gravity="center_horizontal"
    13         android:text="横向显示" />
    14 
    15     <Button
    16         android:id="@+id/vertical"
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:layout_gravity="center_horizontal"
    20         android:paddingTop="20dp"
    21         android:text="竖向显示" />
    22 
    23     <Button
    24         android:id="@+id/singleChoice"
    25         android:layout_width="match_parent"
    26         android:layout_height="wrap_content"
    27         android:layout_gravity="center_horizontal"
    28         android:paddingTop="20dp"
    29         android:text="单选框显示" />
    30 
    31     <Button
    32         android:id="@+id/multiChoice"
    33         android:layout_width="match_parent"
    34         android:layout_height="wrap_content"
    35         android:layout_gravity="center_horizontal"
    36         android:paddingTop="20dp"
    37         android:text="多选框显示" />
    38 
    39     <Button
    40         android:id="@+id/myDialog"
    41         android:layout_width="match_parent"
    42         android:layout_height="wrap_content"
    43         android:layout_gravity="center_horizontal"
    44         android:paddingTop="20dp"
    45         android:text="自定义显示" />
    46 
    47     <ImageView
    48         android:layout_width="match_parent"
    49         android:layout_height="wrap_content"
    50         android:layout_gravity="center_horizontal"
    51         android:layout_weight="1"
    52         android:src="@drawable/ic_launcher" />
    53 
    54 </LinearLayout>

     JAVA代码:

      1 package com.zzw.testalerdialog;
      2 
      3 import android.app.Activity;
      4 import android.app.AlertDialog;
      5 import android.content.Context;
      6 import android.content.DialogInterface;
      7 import android.os.Bundle;
      8 import android.view.View;
      9 import android.widget.Toast;
     10 
     11 public class MainActivity extends Activity implements View.OnClickListener {
     12 
     13     private Context context;
     14 
     15     @Override
     16     protected void onCreate(Bundle savedInstanceState) {
     17         super.onCreate(savedInstanceState);
     18         setContentView(R.layout.activity_main);
     19 
     20         init();
     21     }
     22 
     23     private void init() {
     24 
     25         context = this;
     26 
     27         findViewById(R.id.horizontally).setOnClickListener(this);
     28         findViewById(R.id.vertical).setOnClickListener(this);
     29         findViewById(R.id.singleChoice).setOnClickListener(this);
     30         findViewById(R.id.multiChoice).setOnClickListener(this);
     31         findViewById(R.id.myDialog).setOnClickListener(this);
     32         
     33     }
     34 
     35     // 横向类型显示
     36     private void horizontallyShow() {
     37 
     38         AlertDialog dialog = new AlertDialog.Builder(context).create();
     39         dialog.setIcon(R.drawable.ic_launcher);
     40         dialog.setTitle("横向显示");
     41         dialog.setMessage("提示信息");
     42         // DialogInterface.BUTTON_POSITIVE作用是显示的顺序
     43         dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
     44 
     45             @Override
     46             public void onClick(DialogInterface dialog, int which) {
     47                 Toast.makeText(context, "确定", 0).show();
     48             }
     49         });
     50 
     51         dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
     52 
     53             @Override
     54             public void onClick(DialogInterface dialog, int which) {
     55                 Toast.makeText(context, "取消", 0).show();
     56             }
     57         });
     58 
     59         dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "考虑", new DialogInterface.OnClickListener() {
     60 
     61             @Override
     62             public void onClick(DialogInterface dialog, int which) {
     63                 Toast.makeText(context, "考虑", 0).show();
     64             }
     65         });
     66 
     67         dialog.show();
     68     }
     69 
     70     // 竖向类型显示
     71     private void verticalShow() {
     72 
     73         final String[] items = new String[3];
     74         for (int i = 0; i < 3; i++) {
     75             items[i] = "选项--" + i;
     76         }
     77 
     78         AlertDialog dialog = new AlertDialog.Builder(context).setItems(items, new DialogInterface.OnClickListener() {
     79 
     80             @Override
     81             public void onClick(DialogInterface dialog, int which) {
     82 
     83                 Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
     84             }
     85         }).create();
     86 
     87         dialog.setTitle("竖向显示");
     88         dialog.setIcon(R.drawable.ic_launcher);
     89 
     90         dialog.show();
     91 
     92     }
     93 
     94     // 单选框类型显示
     95     private void singleChoiceShow() {
     96 
     97         final String[] items = new String[3];
     98         for (int i = 0; i < 3; i++) {
     99             items[i] = "选项--" + i;
    100         }
    101 
    102         AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
    103         // mBuilder.setIcon(R.drawable.ic_launcher);
    104         // mBuilder.setTitle("单选框显示");
    105 
    106         // checkedItem默认选择的位置参数
    107         mBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
    108 
    109             @Override
    110             public void onClick(DialogInterface dialog, int which) {
    111                 Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
    112                 dialog.dismiss();
    113 
    114             }
    115         });
    116 
    117         AlertDialog dialog = mBuilder.create();
    118         dialog.setIcon(R.drawable.ic_launcher);
    119         dialog.setTitle("单选框显示");
    120         dialog.show();
    121 
    122     }
    123 
    124     // 复选框类型显示
    125     private void multiChoiceShow() {
    126 
    127         final String[] items = new String[3];
    128         for (int i = 0; i < 3; i++) {
    129             items[i] = "选项--" + i;
    130         }
    131 
    132         AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
    133 
    134         // checkedItems为默认勾选的状态
    135         final boolean[] checkedItems = { false, true, false };
    136         // 这个监听的作用是用于检测item选中状态的变化
    137         mBuilder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
    138 
    139             @Override
    140             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    141 
    142             }
    143         });
    144 
    145         AlertDialog dialog = mBuilder.create();
    146         dialog.setIcon(R.drawable.ic_launcher);
    147         dialog.setTitle("复选框显示");
    148 
    149         // 用于确定,知道用户勾选了那些选项
    150         dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
    151 
    152             @Override
    153             public void onClick(DialogInterface dialog, int which) {
    154 
    155                 String s = "";
    156                 for (int i = 0; i < items.length; i++) {
    157                     if (checkedItems[i])
    158                         s += items[i] + "
    ";
    159                 }
    160                 Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    161             }
    162         });
    163 
    164         // 用于取消
    165         dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
    166 
    167             @Override
    168             public void onClick(DialogInterface dialog, int which) {
    169 
    170             }
    171         });
    172 
    173         dialog.show();
    174     }
    175 
    176     
    177     //自定义类型显示
    178     private void myDialogShow(){
    179         
    180         AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
    181         
    182         mBuilder.setView(getLayoutInflater().inflate(R.layout.my_dialog, null));
    183         
    184         AlertDialog dialog=mBuilder.create();
    185 //        dialog.setIcon(R.drawable.ic_launcher);
    186 //        dialog.setTitle("自定义的对话框");
    187         dialog.show();
    188     }
    189     
    190     @Override
    191     public void onClick(View v) {
    192 
    193         switch (v.getId()) {
    194         case R.id.horizontally:
    195             
    196             horizontallyShow();
    197             break;
    198         case R.id.vertical:
    199             
    200             verticalShow();
    201             break;
    202         case R.id.singleChoice:
    203 
    204             singleChoiceShow();
    205             break;
    206         case R.id.multiChoice:
    207 
    208             multiChoiceShow();
    209             break;
    210         case R.id.myDialog:
    211             myDialogShow();
    212             break;
    213             
    214         }
    215     }
    216 
    217 }

    自定义的布局my_dialog.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:background="#80CBC4"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="vertical" >
     7 
     8     <ImageView
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_gravity="center_horizontal"
    12         android:src="@drawable/ic_launcher" />
    13 
    14     <TextView
    15         android:layout_width="wrap_content"
    16         android:layout_height="wrap_content"
    17         android:layout_gravity="center_horizontal"
    18         android:text="www.cnblogs.com/zzw1994"
    19         android:textColor="@android:color/holo_red_light" />
    20 
    21 </LinearLayout>
  • 相关阅读:
    [netty4][netty-buffer]netty之池化buffer
    [netty4][netty-transport]netty之nio传输层
    JMX基本概念
    《JVM G1源码分析和调优》读书笔记
    clients-producer-网络处理与请求响应对接部分
    clients-producer-组包发送消息
    kafka-clients 1.0 高阶API消费消息(未完)
    MetadataCache更新
    副本同步
    将.py文件装成这执行文件.exe
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5035320.html
Copyright © 2011-2022 走看看