zoukankan      html  css  js  c++  java
  • android四种对话框(dialog)的简单使用方法

    有普通对话框,单选对话框,复选对话框,进度条的两种实现方法话不多说,直接上代码

    activity_main.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context=".MainActivity">
     8     <LinearLayout
     9         android:id="@+id/mainLinearLayout"
    10         android:layout_width="match_parent"
    11         android:layout_height="match_parent"
    12         android:orientation="vertical">
    13         <Button
    14             android:layout_width="wrap_content"
    15             android:layout_height="wrap_content"
    16             android:text="普通对话框"
    17             android:onClick="normalDialog"/>
    18 
    19         <Button
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content"
    22             android:text="单选对话框"
    23             android:onClick="singleDialog"/>
    24         <Button
    25             android:layout_width="wrap_content"
    26             android:layout_height="wrap_content"
    27             android:text="复选对话框"
    28             android:onClick="MultiDialog"/>
    29         <Button
    30             android:layout_width="wrap_content"
    31             android:layout_height="wrap_content"
    32             android:text="进度条对话框"
    33             android:onClick="progressDialog"/>
    34         <Button
    35             android:id="@+id/downlodebutton"
    36             android:layout_width="wrap_content"
    37             android:layout_height="wrap_content"
    38             android:text="进度条对话框"
    39             android:onClick="progressDialog"/>
    40     </LinearLayout>
    41 
    42 </android.support.constraint.ConstraintLayout>

    MainActivity.java:

      1 package com.lgqrlchinese.dialogtext;
      2 
      3 import android.app.ProgressDialog;
      4 import android.content.DialogInterface;
      5 import android.os.SystemClock;
      6 import android.support.v7.app.AlertDialog;
      7 import android.support.v7.app.AppCompatActivity;
      8 import android.os.Bundle;
      9 import android.view.View;
     10 import android.widget.Button;
     11 import android.widget.LinearLayout;
     12 import android.widget.ProgressBar;
     13 import android.widget.Toast;
     14 
     15 
     16 public class MainActivity extends AppCompatActivity {
     17     protected String result;
     18 
     19     @Override
     20     protected void onCreate(Bundle savedInstanceState) {
     21         super.onCreate(savedInstanceState);
     22         setContentView(R.layout.activity_main);
     23         progerssNow();
     24     }
     25 
     26     //普通对话框
     27     public void normalDialog(View view) {
     28         //构建器
     29         AlertDialog.Builder b = new AlertDialog.Builder(this);
     30         //设置标题
     31         b.setTitle("普通对话框");
     32         //设置提示信息
     33         b.setMessage("是否确定退出");
     34         //设置图标
     35         b.setIcon(R.drawable.ic_launcher_background);
     36         //添加确定按钮
     37         b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     38             @Override
     39             public void onClick(DialogInterface dialogInterface, int i) {
     40                 finish();
     41             }
     42         });
     43         //添加取消按钮
     44         b.setNegativeButton("取消", null);
     45         //创建对话框
     46         b.create();
     47         //显示
     48         b.show();
     49     }
     50 
     51     //单选对话框
     52     public void singleDialog(View view) {
     53         AlertDialog.Builder b = new AlertDialog.Builder(this);
     54         b.setTitle("单选对话框");
     55         //选项内容
     56         final String item[] = {"A", "B", "C", "D"};
     57         //setSingleChoiceItems(显示的选项内容,默认值,监听事件)
     58         b.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener() {
     59             @Override
     60             public void onClick(DialogInterface dialogInterface, int i) {
     61                 //取出数组中的数据
     62                 result = item[i];
     63                 //吐司显示出来
     64                 Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
     65             }
     66         });
     67         //设置确定按钮
     68         b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     69             @Override
     70             public void onClick(DialogInterface dialogInterface, int i) {
     71                 Toast.makeText(getApplicationContext(), "选择的结果是" + result, Toast.LENGTH_SHORT).show();
     72             }
     73         });
     74 
     75         b.create();
     76         b.show();
     77     }
     78 
     79     //复选对话框
     80     public void MultiDialog(View view) {
     81         AlertDialog.Builder b = new AlertDialog.Builder(this);
     82         b.setTitle("复选框对话框");
     83         b.setIcon(R.drawable.ic_launcher_background);
     84         //设置选项
     85         final String item[] = {"A", "B", "C", "D"};
     86         //设置默认值
     87         final boolean chechedItem[] = {true, false, true, true};
     88         //设置多选setMultiChoiceItems(选项数组,默认值,监听事件)
     89         b.setMultiChoiceItems(item, chechedItem, new DialogInterface.OnMultiChoiceClickListener() {
     90             @Override
     91             public void onClick(DialogInterface dialogInterface, int i, boolean b) {
     92 
     93             }
     94         });
     95         b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     96             @Override
     97             public void onClick(DialogInterface dialogInterface, int i) {
     98                 StringBuffer stringBuffer = new StringBuffer();
     99                 for (int i1 = 0; i1 < chechedItem.length; i1++) {
    100                     if (chechedItem[i1]) {
    101                         stringBuffer.append(item[i1] + "  ");
    102                     }
    103                 }
    104                 Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_SHORT).show();
    105             }
    106         });
    107         b.create();
    108         b.show();
    109     }
    110 
    111     //进度条对话框
    112     /*
    113      * 在API level 26 中,ProgressDialog被声明不赞成使用,应使用的替代方法是ProgressBar
    114      * */
    115     public void progressDialog(View view) {
    116         final ProgressDialog progressDialog = new ProgressDialog(this);
    117         //设置标题
    118         progressDialog.setTitle("正在加载中》》》");
    119         //设置样式
    120         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    121         //(模拟下载)线程睡眠
    122         new Thread() {
    123             @Override
    124             public void run() {
    125                 //设置最大值
    126                 progressDialog.setMax(100);
    127                 //设置当前值
    128                 for (int i = 0; i < 100; i++) {
    129                     progressDialog.setProgress(i);
    130                     SystemClock.sleep(500);
    131                 }
    132                 progressDialog.dismiss();
    133             }
    134         }.start();
    135         progressDialog.show();
    136     }
    137 
    138     //进度条对话框(推荐使用)
    139     public void progerssNow() {
    140         LinearLayout mainLayout;
    141         Button downlodebutton;
    142         mainLayout = findViewById(R.id.mainLinearLayout);
    143         downlodebutton = findViewById(R.id.downlodebutton);
    144         final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
    145         downlodebutton.setOnClickListener(new View.OnClickListener() {
    146             @Override
    147             public void onClick(View view) {
    148                 new Thread() {
    149                     @Override
    150                     public void run() {
    151 
    152                         progressBar.setMax(100);
    153                         for (int i = 0; i < 100; i++) {
    154                             progressBar.setProgress(i);
    155                             SystemClock.sleep(12);
    156                         }
    157 
    158                     }
    159                 }.start();
    160             }
    161         });
    162         mainLayout.addView(progressBar);
    163     }
    164 }
    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    图表处理ZedGraph
    12月16日
    Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
    (转)EXCEL2007存储格式xlsx
    树型数据结构设计处理
    取得表名以h_开头的表,要使用left,不要使用like
    北京挪动推出神州行5元卡套餐
    手机出口爆炸性增长 品牌输出比例大幅上升
    中电信将进驻国美电器统统门店 启动一体化营销
    联通业务厅开进国美门店
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/9892047.html
Copyright © 2011-2022 走看看