Android開發必知 九種對話方塊的實現方法_Android

來源:互聯網
上載者:User

在開發過程中,與使用者互動式免不了會用到對話方塊以實現更好的使用者體驗,所以掌握幾種對話方塊的實現方法還是非常有必要的。在看具體執行個體之前先對AlertDialog做一個簡單介紹。AlertDialog是功能最豐富、實踐應用最廣的對話方塊,它可以產生各種內容的對話方塊。但實際上AlertDialog產生的對話方塊總體可分為以下4個地區:表徵圖區、標題區、內容區、按鈕區。

這裡總結了九種對話方塊的實現方法,有需要的朋友可以來學習下了

 

除了popupwindow實現稍微麻煩一點,其他形似都相對簡單,熟悉2便即可

直接上源碼

package com.naoh.stu; import java.util.ArrayList; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.PopupWindow; import android.widget.Toast; public class DiaAllActivity extends Activity implements Runnable {   private Button btn_diaNormal;    private Button btn_diaMulti;   private Button btn_diaList;   private Button btn_diaSinChos;   private Button btn_diaMultiChos;   private Button btn_diaProcess;   private Button btn_diaReadProcess;   private Button btn_diaCustom;   private Button btn_popUpDia;   private PopupWindow window=null;   private Button cusPopupBtn1;   private View popupView;   @Override   public void onCreate(Bundle savedInstanceState)   {     super.onCreate(savedInstanceState);     setContentView(R.layout.dialog);     getView();     setListener();   }   private void getView()   {     btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal);     btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti);     btn_diaList=(Button)findViewById(R.id.btn_diaList);     btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos);     btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos);     btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess);     btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess);     btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom);     btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia);   }   private void setListener()   {     btn_diaNormal.setOnClickListener(btnListener);     btn_diaMulti.setOnClickListener(btnListener);     btn_diaList.setOnClickListener(btnListener);     btn_diaSinChos.setOnClickListener(btnListener);     btn_diaMultiChos.setOnClickListener(btnListener);     btn_diaProcess.setOnClickListener(btnListener);     btn_diaReadProcess.setOnClickListener(btnListener);     btn_diaCustom.setOnClickListener(btnListener);     btn_popUpDia.setOnClickListener(btnListener);   }   private Button.OnClickListener btnListener= new Button.OnClickListener()   {     public void onClick(View v)     {       if(v instanceof Button)       {         int btnId=v.getId();         switch(btnId)         {           case R.id.btn_diaNormal:             showNormalDia();             break;           case R.id.btn_diaMulti:             showMultiDia();             break;           case R.id.btn_diaList:             showListDia();             break;           case R.id.btn_diaSigChos:             showSinChosDia();             break;           case R.id.btn_diaMultiChos:             showMultiChosDia();             break;           case R.id.btn_diaReadProcess:             showReadProcess();             break;           case R.id.btn_diaProcess:             showProcessDia();             break;           case R.id.btn_diaCustom:             showCustomDia();             break;           case R.id.btn_popUpDia:             showCusPopUp(v);             break;           default:             break;         }       }     }   };   /*普通的對話方塊*/   private void showNormalDia()   {     //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());     AlertDialog.Builder normalDia=new AlertDialog.Builder(DiaAllActivity.this);     normalDia.setIcon(R.drawable.ic_launcher);     normalDia.setTitle("普通的對話方塊");     normalDia.setMessage("普通對話方塊的message內容");     normalDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         showClickMessage("確定");       }     });     normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         showClickMessage("取消");       }     });     normalDia.create().show();   }   /*多按鈕對話方塊*/   private void showMultiDia()   {     AlertDialog.Builder multiDia=new AlertDialog.Builder(DiaAllActivity.this);     multiDia.setTitle("多選項對話方塊");     multiDia.setPositiveButton("按鈕一", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         showClickMessage("按鈕一");       }     });     multiDia.setNeutralButton("按鈕二", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         showClickMessage("按鈕二");       }     });     multiDia.setNegativeButton("按鈕三", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         showClickMessage("按鈕三");       }     });     multiDia.create().show();   }   /*列表對話方塊*/   private void showListDia()   {     final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};     AlertDialog.Builder listDia=new AlertDialog.Builder(DiaAllActivity.this);     listDia.setTitle("列表對話方塊");     listDia.setItems(mList, new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         /*下標是從0開始的*/         showClickMessage(mList[which]);       }     });     listDia.create().show();   }   /*單項選擇對話方塊*/   int yourChose=-1;   private void showSinChosDia()   {     final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};     yourChose=-1;     AlertDialog.Builder sinChosDia=new AlertDialog.Builder(DiaAllActivity.this);     sinChosDia.setTitle("單項選擇對話方塊");     sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         yourChose=which;       }     });     sinChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         if(yourChose!=-1)         {           showClickMessage(mList[yourChose]);         }       }     });     sinChosDia.create().show();   }   ArrayList<Integer> myChose= new ArrayList<Integer>();   private void showMultiChosDia()   {     final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};     final boolean mChoseSts[]={false,false,false,false,false,false,false};     myChose.clear();     AlertDialog.Builder multiChosDia=new AlertDialog.Builder(DiaAllActivity.this);     multiChosDia.setTitle("多項選擇對話方塊");     multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {       @Override       public void onClick(DialogInterface dialog, int which, boolean isChecked) {         // TODO Auto-generated method stub         if(isChecked)         {           myChose.add(which);         }         else         {           myChose.remove(which);         }       }     });     multiChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         int size=myChose.size();         String str="";         for(int i=0;i<size;i++)         {           str+=mList[myChose.get(i)];         }         showClickMessage(str);       }     });     multiChosDia.create().show();   }   //進度讀取框需要類比讀取   ProgressDialog mReadProcessDia=null;   public final static int MAX_READPROCESS = 100;   private void showReadProcess()   {     mReadProcessDia=new ProgressDialog(DiaAllActivity.this);     mReadProcessDia.setProgress(0);     mReadProcessDia.setTitle("進度條視窗");     mReadProcessDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);     mReadProcessDia.setMax(MAX_READPROCESS);     mReadProcessDia.show();     new Thread(this).start();   }   //新開啟一個線程,迴圈的累加,一直到100然後在停止   @Override   public void run()   {     int Progress= 0;     while(Progress < MAX_READPROCESS)     {       try {         Thread.sleep(100);         Progress++;         mReadProcessDia.incrementProgressBy(1);       } catch (InterruptedException e) {         // TODO Auto-generated catch block         e.printStackTrace();       }     }     //讀取完了以後視窗自消失     mReadProcessDia.cancel();   }   /*讀取中的對話方塊*/   private void showProcessDia()   {     ProgressDialog processDia= new ProgressDialog(DiaAllActivity.this);     processDia.setTitle("進度條框");     processDia.setMessage("內容讀取中...");     processDia.setIndeterminate(true);     processDia.setCancelable(true);     processDia.show();   }   /*自訂對話方塊*/   private void showCustomDia()   {     AlertDialog.Builder customDia=new AlertDialog.Builder(DiaAllActivity.this);     final View viewDia=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.custom_dialog, null);     customDia.setTitle("自訂對話方塊");     customDia.setView(viewDia);     customDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub         EditText diaInput=(EditText) viewDia.findViewById(R.id.txt_cusDiaInput);         showClickMessage(diaInput.getText().toString());       }     });     customDia.create().show();   }   /*popup window 來實現*/   private void showCusPopUp(View parent)   {     if(window == null)     {       popupView=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.dia_cuspopup_dia, null);       cusPopupBtn1=(Button)popupView.findViewById(R.id.diaCusPopupSure);       window =new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);     }     window.setAnimationStyle(R.style.PopupAnimation);     /*必須調用setBackgroundDrawable, 因為popupwindow在初始時,會檢測background是否為null,如果是onTouch or onKey events就不會相應,所以必須設定background*/     /*網上也有很多人說,彈出pop之後,不響應鍵盤事件了,這個其實是焦點在pop裡面的view去了。*/     window.setFocusable(true);     window.setBackgroundDrawable(new BitmapDrawable());      window.update();     window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);     cusPopupBtn1.setOnClickListener(new OnClickListener() {       @Override       public void onClick(View v) {         // TODO Auto-generated method stub         showClickMessage("popup window的確定");       }     });   }   /*顯示點擊的內容*/   private void showClickMessage(String message)   {     Toast.makeText(DiaAllActivity.this, "你選擇的是: "+message, Toast.LENGTH_SHORT).show();   } } 

布局,就是一堆的button

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical" >   <TextView     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="各種Dialog合集" />   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="普通Dialog"     android:id="@+id/btn_diaNormal"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="多按鈕Dialog"     android:id="@+id/btn_diaMulti"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="列表Dialog"     android:id="@+id/btn_diaList"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="單項選擇Dialog"     android:id="@+id/btn_diaSigChos"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="多項選擇Dialog"     android:id="@+id/btn_diaMultiChos"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="進度條Dialog"     android:id="@+id/btn_diaReadProcess"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="讀取中Dialog"     android:id="@+id/btn_diaProcess"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="自訂Dialog"     android:id="@+id/btn_diaCustom"/>   <Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="PopUpWindow實現的dialog"     android:id="@+id/btn_popUpDia"/> </LinearLayout> 

以上是Android開發必知 九種對話方塊的實現方法,代碼主要用AlertDialog產生的對話方塊,它的功能豐富、使用最廣。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.