国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android AlertDialog多種使用方法詳解

2019-12-12 04:01:50
字體:
來源:轉載
供稿:網友

當你的應用需要顯示一個進度條或需要用戶對信息進行確認時,可以使用alertDialog來完成。下面來介紹常用的四種AlertDialog。

1、普通對話框

package com.example.yk.dialogtest;  import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast;  /**  * AlertDialog普通對話框  */ public class GeneralDialogActivity extends AppCompatActivity {   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_general_dialog);   AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)     .setTitle("操作title")//設置title     .setMessage("操作message")//設置要顯示的message     .setCancelable(false)//表示點擊dialog其它部分不能取消(除了“取消”,“確定”按鈕)     .setPositiveButton("確定", new       DialogInterface.OnClickListener() {    @Override    public void onClick(DialogInterface dialogInterface, int i) {     Toast.makeText(GeneralDialogActivity.this, "點擊了確定", Toast.LENGTH_SHORT).show();     }   }).setNegativeButton("取消", new DialogInterface.OnClickListener() {    @Override    public void onClick(DialogInterface dialogInterface, int i) { //    dialogInterface.dismiss();    }   });   alertDialog.show();//別忘了show  } } 

2、單選對話框

package com.example.yk.dialogtest;  import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast;  /**  * 單選對話框  */ public class SingleDialogActivity extends AppCompatActivity {  private String[] items={"java","php","c"};  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_single_dialog);   AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)     .setTitle("提示title") //    .setMessage("提示message")//在需要設置單選對話框的情況下是不能設置message的,否則單選對話框內容會失效     .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {//checkedItem=-1表示默認不選中      @Override      public void onClick(DialogInterface dialogInterface, int i) {       Toast.makeText(SingleDialogActivity.this, "選中了"+items[i], Toast.LENGTH_SHORT).show();      }     }).setPositiveButton("確定", new DialogInterface.OnClickListener() {      @Override      public void onClick(DialogInterface dialogInterface, int i) {       }     });    alertDialog.show();  } } 

3、多選對話框

package com.example.yk.dialogtest;  import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast;  /**  * 多選對話框  */ public class MultiChoiceDialogActivity extends AppCompatActivity {  private String[] items={"java","php","c"};  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_multi_choice_dialog);   AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)     .setTitle("提示title")     .setCancelable(false)     .setMultiChoiceItems(items, new boolean[]{false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {        @Override      public void onClick(DialogInterface dialogInterface, int i, boolean b) {       if(b){        Toast.makeText(MultiChoiceDialogActivity.this, "選中了"+items[i], Toast.LENGTH_SHORT)          .show();       }       }     })     .setPositiveButton("確定", new DialogInterface.OnClickListener() {      @Override      public void onClick(DialogInterface dialogInterface, int i) {       }     });   alertDialog.show();  } } 

4、進度條對話框

package com.example.yk.dialogtest;  import android.app.ProgressDialog; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;  /**  * 進度條對話框  */ public class ProgressDialogActivity extends AppCompatActivity {  private ProgressDialog progressDialog;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_progress_dialog);   progressDialog = new ProgressDialog(this);   progressDialog.setTitle("提示title");   progressDialog.setCancelable(true); //  progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//旋轉進度條,默認風格   progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//橫向進度條   progressDialog.show();   } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汕尾市| 正定县| 海兴县| 景东| 贵定县| 石屏县| 耒阳市| 杭锦后旗| 上蔡县| 兰溪市| 泰顺县| 五河县| 陆丰市| 洛宁县| 博爱县| 泸溪县| 安福县| 额尔古纳市| 铜陵市| 金塔县| 子长县| 蓝山县| 包头市| 琼海市| 东宁县| 兴义市| 五峰| 屏南县| 游戏| 文安县| 建昌县| 梅州市| 巴南区| 东安县| 长兴县| 镇坪县| 新丰县| 广东省| 沾益县| 曲周县| 诏安县|