您好,登錄后才能下訂單哦!
下面代碼寫了常見的幾個對話框,包含:
1、確定取消對話框
2、單選對話框
3、多選對話框
4、進度對話框(加載時的)
5、下載進度對話框
package com.yuanlp.dialogs; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 創建一個確定取消對話框 * @param view */ public void click1(View view){ //工廠設計模式,獲取對話框工廠 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("警告"); builder.setMessage("欲練此功,必先自宮"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"點擊了是",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"不自宮,不成功",Toast.LENGTH_SHORT).show(); } }); //一定要顯示show,不然不會顯示 builder.show(); } /** * 創建一個單選對話框 * @param view */ public void click2(View view){ //工廠設計模式,獲取對話框工廠 final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇姓名"); final String[] items=new String[]{"男","女","未知"}; builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"選擇的性別是:"+items[which],Toast.LENGTH_LONG).show(); //關閉對話框 dialog.dismiss(); } }); //一定要顯示show,不然不會顯示 builder.show(); } /** * 創建一個多選對話框 * @param view */ public void click3(View view){ //工廠設計模式,獲取對話框工廠 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("警告"); final String[] items={"蘋果","香蕉","梨子","桃子"}; final boolean[] checkdes={false,true,true,true}; //參數解析:第一個是多選項的一個集合,第二個是每個選項默認的選擇狀態,第三個是監聽 builder.setMultiChoiceItems(items, checkdes, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this,items[which]+isChecked,Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb=new StringBuffer(); for(int i=0;i<checkdes.length;i++){ if (checkdes[i]){ sb.append(items[i]+" "); } } Toast.makeText(MainActivity.this,"喜歡吃的水果是:"+sb.toString(),Toast.LENGTH_SHORT).show(); } }); //一定要顯示show,不然不會顯示 builder.show(); } /** * 創建一個進度對話框 * @param view */ public void click4(View view){ //工廠設計模式,獲取對話框工廠 final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("稍等"); pd.setMessage("正在拼命加載中"); //一定要顯示show,不然不會顯示 pd.show(); new Thread(){ @Override public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } pd.dismiss(); //關閉對話框 } }.start(); } /** * 創建具體進度條的對話框 * @param view */ public void click5(View view){ //工廠設計模式,獲取對話框工廠 final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("請等待"); pd.setMessage("正在拼命加載"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(100); //一定要顯示show,不然不會顯示 pd.show(); new Thread(){ @Override public void run() { try { for (int i=0;i<100;i++){ Thread.sleep(300); pd.setProgress(i); } } catch (InterruptedException e) { e.printStackTrace(); } pd.dismiss(); } }.start(); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。