• Android常用的Dialog对话框用法


    Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4,

    V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog。

      1 import android.app.ProgressDialog;
      2 import android.content.DialogInterface;
      3 import android.os.Bundle;
      4 import android.os.SystemClock;
      5 import android.support.v7.app.AlertDialog;
      6 import android.support.v7.app.AppCompatActivity;
      7 import android.view.View;
      8 import android.widget.Toast;
      9 
     10 public class MainActivity extends AppCompatActivity {
     11 
     12     @Override
     13     protected void onCreate(Bundle savedInstanceState) {
     14         super.onCreate(savedInstanceState);
     15         setContentView(R.layout.activity_main);
     16     }
     17 
     18     /**
     19      * 普通对话框
     20      *
     21      * @param view
     22      */
     23     public void click1(View view) {
     24         AlertDialog.Builder builder = new AlertDialog.Builder(this);
     25         //设置标题
     26         builder.setTitle("用户安全提示:");
     27         //设置提示消息
     28         builder.setMessage("你确定接受用户隐私条款吗?");
     29         builder.setPositiveButton("确定", null);
     30         builder.setNegativeButton("取消", null);
     31         //显示dialog
     32         builder.show();
     33     }
     34 
     35     /**
     36      * 单选对话框
     37      *
     38      * @param view
     39      */
     40     public void click2(View view) {
     41         AlertDialog.Builder builder = new AlertDialog.Builder(this);
     42         //设置标题
     43         builder.setTitle("选择一项你的专业:");
     44         //设置要显示的item条目
     45         final String items[] = {"Java", "C++", "PHP", "Android", "C#", "Python", "MySql"};
     46         //设置item点击事件
     47         builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
     48             @Override
     49             public void onClick(DialogInterface dialog, int which) {
     50                 Toast.makeText(MainActivity.this, "你选择了:" + items[which], Toast.LENGTH_SHORT).show();
     51                 dialog.dismiss();
     52             }
     53         });
     54         //显示dialog
     55         builder.show();
     56     }
     57 
     58     /**
     59      * 多选对话框
     60      *
     61      * @param view
     62      */
     63     public void click3(View view) {
     64         AlertDialog.Builder builder = new AlertDialog.Builder(this);
     65         //设置标题
     66         builder.setTitle("选择你的兴趣爱好:");
     67         //设置要显示的item条目
     68         final String items[] = {"看书", "玩游戏", "看电影", "打球", "健身", "自驾游", "去酒吧"};
     69         //设置每个条目的选择状态 false为没有选中 true为选中
     70         final boolean[] checks = {true, true, false, false, false, false, false};
     71         //设置item点击事件 条目被选中时 修改checks里的boolean值
     72         builder.setMultiChoiceItems(items, checks, new DialogInterface.OnMultiChoiceClickListener() {
     73             @Override
     74             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
     75                 checks[which] = isChecked;
     76             }
     77         });
     78         //设置确定按钮点击事件
     79         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     80             @Override
     81             public void onClick(DialogInterface dialog, int which) {
     82                 StringBuffer buffer = new StringBuffer();
     83                 for (int i = 0; i < checks.length; i++) {
     84                     if (checks[i]) {
     85                         buffer.append(items[i] + ",");
     86                     }
     87                 }
     88                 Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
     89             }
     90         });
     91         builder.setNegativeButton("取消", null);
     92         //显示dialog
     93         builder.show();
     94     }
     95 
     96     /**
     97      * 进度条对话框
     98      *
     99      * @param view
    100      */
    101     public void click4(View view) {
    102         final ProgressDialog dialog = new ProgressDialog(this);
    103         dialog.setTitle("请稍等:");
    104         //dialog.setMessage("正在努力加载...");
    105         //dialog.show();
    106 
    107         //以下的属性不设置 进度条样式默认为圆形
    108 
    109         //设置进度最大值为100
    110         dialog.setMax(100);
    111         //设置进度条样式为水平
    112         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    113 
    114         dialog.show();
    115         //模拟一个进度加载过程
    116         new Thread() {
    117             @Override
    118             public void run() {
    119                 for (int i = 0; i <= 100; i++) {
    120                     dialog.setProgress(i);
    121                     //休眠200毫秒
    122                     SystemClock.sleep(200);
    123                 }
    124                 dialog.dismiss();
    125             }
    126         }.start();
    127     }
    128 }

  • 相关阅读:
    Binary Trees
    [POJ] String Matching
    Tree
    Maxmum subsequence sum problem
    poj 2104 划分树
    poj 2486 树形dp
    poj 1848 树形dp
    hdu 4578 线段树
    hdu 4585 set应用
    hdu 2412 树形DP
  • 原文地址:https://www.cnblogs.com/hejiaoshou/p/7468984.html
Copyright © 2020-2023  润新知