詳解Dialog(一)——基礎元素構建

本篇內容比較簡單,是最基礎篇,包括對話框標題、消息、按鈕、內容視圖的動態構建和靜態構建方法。

 

一、標題、消息、按鈕

首先我們要認識一個函數:

AlertDialog.Builder builder = new AlertDialog.Builder(context);	

這個AlertDialog.Builder(context)就是用來構建Dialog各種相關參數的,跟StringBuilder的含義差不多,在全部想要的參數都添加上完了以後,調用:

 

Builder.create()來創建對話框,使用Builder.show()來顯示對話框。

Builder.create();
Builder.show();

對話框的構建與顯示大致就是這個樣子,下面讓我們來看看AlertDialog.Builder(context);的各種參數;

1、ICON、標題與消息

先看看對話框中ICON、標題與消息各自的位置:

添加他們的代碼分別是:

builder.setIcon(R.drawable.ic_launcher);//添加ICON
builder.setTitle("退出");                //添加標題
builder.setMessage("你確定要離開嗎?");     //添加MSG

所以要構建上圖的對對話框的完整代碼是這樣的:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);//添加ICON
builder.setTitle("退出");                //添加標題
builder.setMessage("你確定要離開嗎?");     //添加MSG
builder.create();
builder.show();

出來的效果圖是這樣的:

2、按鈕

google爲我們提供了三個按鈕選擇,左按鈕,右按鈕和中間按鈕,分別對應:

Builder.setPositiveButton(string,DialogInterface.OnClickListener);//確定按鈕
Builder.setNegativeButton(string,DialogInterface.OnClickListener);//取消按鈕
Builder.setNeutralButton(string,DialogInterface.OnClickListener);//中間按鈕

這裏需要注意的是,中間按鈕(setNeutralButton)無論把它放在代碼的什麼位置,他始終是在中間的,跟代碼的具體位置無關,其實這個幾按鈕函數的代碼的按鈕位置跟他們的代碼位置是都沒有關係的,並不會因爲在代碼中先寫了確定按鈕,在顯示時確定按鈕就在左邊,不是這樣的,它是跟系統樣式相關聯的,系統樣式中定義的確定按鈕在哪邊顯示它就會在哪邊顯示!
 

看一下他們具體使用:

builder.setPositiveButton("確定",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this, "ok click",Toast.LENGTH_LONG).show();
			}
		});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this,"cancel click", Toast.LENGTH_LONG).show();
			}
		});
builder.setNeutralButton("中間BTN",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this,"center click", Toast.LENGTH_LONG).show();
			}
		});

我們在每次點擊時都彈一個Tost出來,完整的代碼應當是這樣的:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);//添加ICON
builder.setTitle("退出");                //添加標題
builder.setMessage("你確定要離開嗎?");     //添加MSG
 
builder.setPositiveButton("確定",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this, "ok click",Toast.LENGTH_LONG).show();
			}
		});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this,"cancel click", Toast.LENGTH_LONG).show();
			}
		});
builder.setNeutralButton("中間BTN",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int whichButton) {
				// 點擊響應
				Toast.makeText(MainActivity.this,"center click", Toast.LENGTH_LONG).show();
			}
		});
builder.create();
builder.show();

效果是這樣的:

在這裏可以看出,在代碼中我們先寫的確定按鈕,而最左邊確是取消按鈕,所以按鈕的顯示位置只與系統樣式有關,而與代碼中哪個提前寫沒有任何關係。

細心的同學可能會看到在DialogInterface.OnClickListener()的(DialogInterface dialog,int whichButton) {}中有兩個參數,第一個是當前的Dialog,第二個是表示當前點擊的哪個按鈕的which,在這裏我們用不到witchButton這個參數,因爲我們直接在每個按鈕的響應部位添加了響應代碼,如果我們在整個類中添加實現了DialogInterface.OnClickListener()接口,那我們在做出點擊響應時就應該判斷當前是哪個按鈕點擊了,這三個按鈕的ID分別對應:

BUTTON_POSITIVE  //確定按鈕
BUTTON_NEGATIVE  //取消按鈕
BUTTON_NEUTRAL    //中間按鈕

上面說的那些東東的意思是這樣的:
 

public class MainActivity extends Activity implements DialogInterface.OnClickListener {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setPositiveButton("確定",this);
		builder.setNegativeButton("取消",this);
		builder.setNeutralButton("中間BTN",this);
		builder.create();
		builder.show();
 
	}
 
	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		switch (which) {
		case DialogInterface.BUTTON_POSITIVE:
			//todo 確定按鈕響應
			break;
		case DialogInterface.BUTTON_NEGATIVE:
			//todo 取消按鈕響應
			break;
		case DialogInterface.BUTTON_NEUTRAL:
			//todo 中間按鈕響應
			break;
		default:
			break;
		}
	}
}

這裏在一個Activity中繼承了DialogInterface.OnClickListener接口,我們在Builder.setpositiveButton的時候直接將監聽函數設爲this,所以在響應的時候要判斷當前是哪個按鈕過來的,這種方法很少使用,這裏只是簡單說一下DialogInterface.OnClickListener()中witch的含義及用法。
到這裏,我們就講完了,一個對話框應具有的幾個基本屬性,下面我們看看如果我想加個ImageView,TextView等等的佈局要怎麼辦?下面就是自定義佈局的部分了。

 

二、自定義佈局

在AlertDialog中是允許我們傳入我們自己的佈局視圖的,只需要將我們的View通過:

Builder.setView(view);

設置進去就可以了,下面就是我們如何構建我們的視圖的問題了,這裏提供兩種方法,靜態構建和動態構建;

 

1、靜態構建佈局

首先,我們可以隨意寫一個Layout文件,比如下面這個:(custom_view.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_view_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="小狗狗的照片"/>
    
 
</LinearLayout>

視覺是這樣的:

 

在佈局寫好之後,通過下面LayoutInflater就可以得到他的View對象,然後通過Builder.setView(view)設置進Dialog:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.custom_view, null);
builder.setView(view);

下面我們就完整的寫下這段代碼,給Dialog添加上ICON,標題,MSG和View,按鈕就不加了,因爲代碼太多,影響解讀

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);//添加ICON
builder.setTitle("退出");                //添加標題
builder.setMessage("你確定要離開嗎?");     //添加MSG
 
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.custom_view, null);
 
builder.setView(view);//添加自定義View
builder.create();
builder.show();

出來的效果是這樣的:

 

2、動態構建佈局

現在我們要用代碼來動態構建一個與上一個小狗狗相同的佈局,有關動態創建佈局

LinearLayout root_lin=new LinearLayout(MainActivity.this);  
root_lin.setOrientation(LinearLayout.HORIZONTAL);  
LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
root_lin.setLayoutParams(LP_FW);
 
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.dog);
imageView.setLayoutParams(LP_FW);
root_lin.addView(imageView);
 
TextView tv = new TextView(MainActivity.this);  
tv.setText("可愛的狗狗");  
tv.setTextSize(20);  
tv.setLayoutParams(LP_FW);  
root_lin.addView(tv); 

這裏首先是創建一個線性的根佈局root_lin,然後把新建的imageView和Text控件都添加到裏面,尤其要關注的是添加時的參數:

LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

這裏爲什麼給imageView和textview設置佈局參數時全部是使用LinearLayout.LayoutParams構建的呢?這是因爲,每個控件的佈局參數都是依據父控件來構建的,如果父控件是線性佈局那就用線性佈局來構建參數,如果是相對佈局那就要用下面這個:(這個是layout_width="wrap_cotent",layou_heigt="wrap_content");
 

RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(
		RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

同理,如果父控件是幀佈局(FrameLayout),那構建參數就要使用幀佈局的參數來構建了:(這裏構建的是:layout_width="fill_parent",layout_hight = "wrap_content")

FrameLayout.LayoutParams FL_FW = new FrameLayout.LayoutParams(
		FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);

同樣,在構建完佈局,直接通過Builder.setView(root_lin),將佈局的根結點設置進去就可以了,完整的代碼如下:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);//添加ICON
builder.setTitle("退出");                //添加標題
builder.setMessage("你確定要離開嗎?");     //添加MSG
 
LinearLayout root_lin=new LinearLayout(MainActivity.this);  
root_lin.setOrientation(LinearLayout.HORIZONTAL);  
LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
root_lin.setLayoutParams(LP_FW);
 
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.dog);
imageView.setLayoutParams(LP_FW);
root_lin.addView(imageView);
 
TextView tv = new TextView(MainActivity.this);  
tv.setText("可愛的狗狗");  
tv.setTextSize(20);  
tv.setLayoutParams(LP_FW);  
root_lin.addView(tv); 
 
builder.setView(root_lin);
builder.create();
builder.show();

效果與上一個圖一樣,就不貼了。

 

好了,這篇到這就結束了,下篇給大家說說有關ListView的Dialog的東東。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章