Android中經常會使用到Dialog(彈出框效果),而且功能非常強大,可以類比出N種彈出框效果。:
下面將通過一個小執行個體,來像大家展示Android中功能強大的Dialog。代碼都寫了詳細的注釋,讀者不妨試著手動去敲。
當然,由於時間的關係,還有個別功能沒有完成,先有的幾個展示效果中也可能有bug,代碼也有很多需要最佳化的地方。望大家熱心指出。
下面是代碼(有點長哦):
package com.chaoyang.activity;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { /** Called when the activity is first created. */final String[] items={"張三","李四","王五","趙六","阿貓","阿狗"}; int mSingleChoiceID = -1; ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); private ProgressDialog mProgressDialog = null; private static final int MAX_PROGRESS = 100; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*START 設定那些按鈕的點擊監聽對象*/ ButtonOnclikListen listen =new ButtonOnclikListen(); Button btnConfim =(Button)this.findViewById(R.id.btnConfim); Button btnCheck =(Button)this.findViewById(R.id.btnCheck); Button btnCustom =(Button)this.findViewById(R.id.btnCustom); Button BtnList =(Button)this.findViewById(R.id.BtnList); Button btnMore =(Button)this.findViewById(R.id.btnMore); Button BtnProgressbar =(Button)this.findViewById(R.id.BtnProgressbar); Button btnRead =(Button)this.findViewById(R.id.btnRead); Button btnRidio =(Button)this.findViewById(R.id.btnRidio); btnConfim.setOnClickListener(listen); btnCheck.setOnClickListener(listen); btnConfim.setOnClickListener(listen); BtnList.setOnClickListener(listen); btnMore.setOnClickListener(listen); BtnProgressbar.setOnClickListener(listen); btnRead.setOnClickListener(listen); btnRidio.setOnClickListener(listen); /* END */ } //定義一個公用方法,彈出訊息 private void showDialogMSG(String msg) { new AlertDialog.Builder(MainActivity.this) .setMessage(msg) .show(); } //定義一個內部類,繼承至OnClickListener,專門來處理按鈕點擊事件(物件導向,靈活) private final class ButtonOnclikListen implements View.OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubButton button =(Button) v;//接收被點擊的Button對象AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);//定義一個彈出框對象builder.setIcon(R.drawable.aa);//設定彈出框的ICONswitch (v.getId()){case R.id.btnConfim :builder.setTitle("確定提交嗎?");//設定彈出框的標題builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {showDialogMSG("您選擇了確定");}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {showDialogMSG("您選擇了取消");}});break;case R.id.btnMore:builder.setTitle("評價");//標題builder.setMessage("說說李孝利在你心裡的印象");//彈出框訊息builder.setPositiveButton("性感",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub showDialogMSG("您覺得她很性感");}});builder.setNeutralButton("漂亮", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub showDialogMSG("您覺得她很漂亮");}});builder.setNegativeButton("風騷", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubshowDialogMSG("您覺得她很風騷");}});break;case R.id.BtnList :builder.setTitle("列表框");builder.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubshowDialogMSG("您選擇了ID為"+which+"的人,名字叫:"+items[which]);}});break;case R.id.btnRidio: mSingleChoiceID = -1;builder.setTitle("單選框");builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubmSingleChoiceID=which;showDialogMSG("您選擇了ID是"+mSingleChoiceID+",名字為:"+items[which]);}});builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubif(mSingleChoiceID>-1){showDialogMSG("您最終選擇的是"+items[mSingleChoiceID]);}else{showDialogMSG("您最終選擇的是"+items[0]);}}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});break;case R.id.btnCheck :MultiChoiceID.clear(); //先清空集合builder.setTitle("多項選擇");builder.setMultiChoiceItems(items,new boolean[]{false,false,false,false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){MultiChoiceID.add(which);showDialogMSG("您選擇了"+items[which]);}else{MultiChoiceID.remove(which);}}});builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubString str="";for(int i = 0;i<MultiChoiceID.size();i++){str+=items[MultiChoiceID.get(i)]+",";}//去掉最後一個","if(str.length()>0){str = str.substring(0,str.length()-1);}showDialogMSG("你一共選擇的有"+str);}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});break;case R.id.BtnProgressbar:mProgressDialog =new ProgressDialog(MainActivity.this);mProgressDialog.setTitle("進度條視窗");mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定樣式mProgressDialog.setMax(MAX_PROGRESS);//設定最大值 mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //這裡添加點擊後的邏輯 } }); mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //這裡添加點擊後的邏輯 } }); mProgressDialog.show(); new Thread().start(); return;case R.id.btnCustom: break;case R.id.btnRead:break;}builder.create().show();//建立並顯示彈出框} } }
布局檔案部分,比較簡單:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btnConfim" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="確定/取消" /> <Button android:id="@+id/btnMore" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多個訊息提示" /> <Button android:id="@+id/BtnList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表框" /> <Button android:id="@+id/BtnProgressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="進度條框" /> <Button android:id="@+id/btnRidio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="單選框" /> <Button android:id="@+id/btnCheck" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多選框" /> <Button android:id="@+id/btnCustom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自訂布局" /> <Button android:id="@+id/btnRead" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="讀取進度框" /></LinearLayout>