欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android AlertDialog多種使用方法詳解

 更新時間:2017年01月14日 11:43:12   作者:愛dy  
這篇文章主要為大家詳細(xì)介紹了android AlertDialog多種使用方法,包括普通對話框、單選對話框、多選對話框等,具有一定的參考價值,感興趣的小伙伴們可以參考一下

當(dāng)你的應(yīng)用需要顯示一個進(jìn)度條或需要用戶對信息進(jìn)行確認(rèn)時,可以使用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")//設(shè)置title 
    .setMessage("操作message")//設(shè)置要顯示的message 
    .setCancelable(false)//表示點(diǎn)擊dialog其它部分不能取消(除了“取消”,“確定”按鈕) 
    .setPositiveButton("確定", new 
      DialogInterface.OnClickListener() { 
   @Override 
   public void onClick(DialogInterface dialogInterface, int i) { 
    Toast.makeText(GeneralDialogActivity.this, "點(diǎn)擊了確定", 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")//在需要設(shè)置單選對話框的情況下是不能設(shè)置message的,否則單選對話框內(nèi)容會失效 
    .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {//checkedItem=-1表示默認(rèn)不選中 
     @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、進(jìn)度條對話框

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; 
 
/** 
 * 進(jìn)度條對話框 
 */ 
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);//旋轉(zhuǎn)進(jìn)度條,默認(rèn)風(fēng)格 
  progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//橫向進(jìn)度條 
  progressDialog.show(); 
 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論