• Android对话框总结


      安卓内置了四种对话框:AlertDialog、ProgressDialog、DataPickerDialog、TimePickerDialog.这几种对话框外观都是系统定义好的,如果我们想自定义外观,我们可以利用AlertDialog自定义对话框。本文主要介绍AlertDialog及其自定义,其它的对话框说白了都是一些控件,对于控件的学习个人觉得先大致了解如何去用就行了,等到真正用到的时候再去详细学习是最合适的。

    AlertDialog

      步骤:1.创建AlertDialog.Builder对象builder

         2.利用builder为Dialog设置各种属性,如:标题、内容、图标

           3.调用builder的create创建对话框

         4.调用AlertDialog的show方法,显示对话框

    1.创建提示消息对话框

    例子很简单就一个button,点击此button显示一个对话框

    View Code
     1 findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
     2 
     3             @Override
     4             public void onClick(View v) {
     5                 // create dialog...
     6                 AlertDialog.Builder builder = new Builder(MainActivity.this);
     7                 builder.setTitle("AlertDialog");
     8                 builder.setMessage("...");
     9                 builder.setPositiveButton("ok",
    10                         new DialogInterface.OnClickListener() {
    11 
    12                             @Override
    13                             public void onClick(DialogInterface dialog,
    14                                     int which) {
    15                                 dialog.dismiss();
    16                             }
    17                         });
    18                 builder.create().show();
    19             }
    20         });

    2.创建列表对话框

      AlertDialog支持三种类型的列表,1.传统单选列表 2.带radiobutton的列表对话框 3.多选列表对话框 。

    View Code
      1 package com.example.dialog;
      2 
      3 import android.os.Bundle;
      4 import android.view.View;
      5 import android.view.View.OnClickListener;
      6 import android.widget.Toast;
      7 import android.app.Activity;
      8 import android.app.AlertDialog;
      9 import android.app.AlertDialog.Builder;
     10 import android.content.Context;
     11 import android.content.DialogInterface;
     12 import android.content.DialogInterface.OnMultiChoiceClickListener;
     13 import android.content.res.Resources;
     14 
     15 public class MainActivity extends Activity {
     16 
     17     private Context context = this;
     18 
     19     @Override
     20     protected void onCreate(Bundle savedInstanceState) {
     21         super.onCreate(savedInstanceState);
     22         setContentView(R.layout.activity_main);
     23         // 提示消息对话框
     24         findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
     25 
     26             @Override
     27             public void onClick(View v) {
     28                 // create dialog...
     29                 AlertDialog.Builder builder = new Builder(context);
     30                 builder.setTitle("AlertDialog");
     31                 builder.setMessage("提示消息对话框");
     32                 builder.setPositiveButton("ok",
     33                         new DialogInterface.OnClickListener() {
     34 
     35                             @Override
     36                             public void onClick(DialogInterface dialog,
     37                                     int which) {
     38                                 dialog.dismiss();
     39                             }
     40                         });
     41                 builder.create().show();
     42             }
     43         });
     44 
     45         // 单选列表对话框
     46         findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
     47 
     48             @Override
     49             public void onClick(View v) {
     50                 AlertDialog.Builder builder = new Builder(context);
     51                 builder.setTitle("AlertDialog");
     52                 builder.setItems(R.array.colors,
     53                         new DialogInterface.OnClickListener() {
     54 
     55                             @Override
     56                             public void onClick(DialogInterface dialog,
     57                                     int which) {
     58                                 Resources res = context.getResources();
     59                                 String str[] = res
     60                                         .getStringArray(R.array.colors);
     61                                 Toast.makeText(context, str[which],
     62                                         Toast.LENGTH_SHORT).show();
     63                             }
     64                         });
     65                 builder.create().show();
     66             }
     67         });
     68 
     69         // 单选按钮对话框
     70         findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
     71 
     72             @Override
     73             public void onClick(View v) {
     74                 AlertDialog.Builder builder = new Builder(context);
     75                 builder.setTitle("AlertDialog");
     76                 builder.setSingleChoiceItems(R.array.colors, 0,
     77                         new DialogInterface.OnClickListener() {
     78 
     79                             @Override
     80                             public void onClick(DialogInterface dialog,
     81                                     int which) {
     82                                 Resources res = context.getResources();
     83                                 String str[] = res
     84                                         .getStringArray(R.array.colors);
     85                                 Toast.makeText(context, str[which],
     86                                         Toast.LENGTH_SHORT).show();
     87                             }
     88                         });
     89                 builder.setPositiveButton("ok",
     90                         new DialogInterface.OnClickListener() {
     91 
     92                             @Override
     93                             public void onClick(DialogInterface dialog,
     94                                     int which) {
     95                                 dialog.dismiss();
     96                             }
     97                         });
     98                 builder.create().show();
     99             }
    100         });
    101 
    102         // 多选列表对话框
    103         findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {
    104 
    105             @Override
    106             public void onClick(View v) {
    107                 AlertDialog.Builder builder = new Builder(context);
    108                 builder.setTitle("AlertDialog");
    109                 final boolean[] checkStatus = new boolean[] { true, false,
    110                         true, false };
    111                 builder.setMultiChoiceItems(R.array.colors, checkStatus,
    112                         new OnMultiChoiceClickListener() {
    113                             Resources res = context.getResources();
    114                             String str[] = res.getStringArray(R.array.colors);
    115 
    116                             @Override
    117                             public void onClick(DialogInterface dialog,
    118                                     int which, boolean isChecked) {
    119                                 Toast.makeText(context, str[which],
    120                                         Toast.LENGTH_SHORT).show();
    121                             }
    122                         });
    123                 builder.create().show();
    124             }
    125         });
    126     }
    127 }

    3.自定义对话框

      1.为对话框设置布局文件

      2.创建AlertDialog.Builder对象builder

      3.实例化LayoutInflater对象:LayoutInflater inflater = XXXActivity.getLayoutInflater();

      4.调用inflater的inflate方法:inflater.inflate(R.layout.dialog_layout,viewgroup);

      5.调用builder.setView()方法

      6.调用builder的create创建对话框

      7.调用AlertDialog的show方法,显示对话框

    例子:布局文件

    View Code

    关键代码:

    View Code
     1 // 自定义列表对话框
     2         findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {
     3 
     4             @Override
     5             public void onClick(View v) {
     6                 AlertDialog.Builder builder = new Builder(context);
     7                 builder.setTitle("CustomDialog");
     8                 LayoutInflater inflater = context.getLayoutInflater();
     9                 ViewGroup viewgroup = (ViewGroup) findViewById(R.id.Dialog_root);
    10                 final View view = inflater.inflate(R.layout.dialog_layout,
    11                         viewgroup);
    12                 builder.setView(view);
    13                 final EditText username = (EditText) view
    14                         .findViewById(R.id.username);
    15                 final EditText password = (EditText) view
    16                         .findViewById(R.id.password);
    17 
    18                 builder.setPositiveButton("show",
    19                         new DialogInterface.OnClickListener() {
    20 
    21                             @Override
    22                             public void onClick(DialogInterface dialog,
    23                                     int which) {
    24                                 Toast.makeText(
    25                                         context,
    26                                         username.getText().toString() + ";"
    27                                                 + password.getText().toString(),
    28                                         Toast.LENGTH_SHORT).show();
    29                             }
    30                         });
    31                 builder.create().show();
    32             }
    33         });

    PS:有新发现会陆续补充进来...

      

  • 相关阅读:
    GridView合并表头多重表头
    C# 导出Excel或Word
    GridView的分页功能?
    如何在GridView中判断Radio被选中?
    GridView無數據時,顯示表頭
    Oracle replace函数使用
    获取数据后导出Excel
    Oracel用rownum实现真分页
    转载C#泛型集合—Dictionary<K,V>使用技巧
    临时向表插入有自增的字段的记录
  • 原文地址:https://www.cnblogs.com/byghui/p/3053677.html
Copyright © 2020-2023  润新知