本文來自http://blog.csdn.net/hellogv/ ,引用必須註明出處!
時隔一年,又要準備做Android的開發了,最近複習和整理一下Android的知識。這次要說的是AlertDialog,這種對話方塊會經常遇到。AlertDialog跟WIN32開發中的Dialog不一樣,AlertDialog是非阻塞的,而阻塞的對話方塊用的是PopupWindow。
先貼出程式啟動並執行:
main.xml的源碼:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ></p><p><Button android:id="@+id/Button01" android:layout_height="wrap_content" android:text="非Layout型對話方塊" android:layout_width="fill_parent"></Button><br /><Button android:id="@+id/Button02" android:layout_height="wrap_content" android:text="Layout型對話方塊" android:layout_width="fill_parent"></Button><View android:id="@+id/View01" android:layout_width="wrap_content" android:layout_height="wrap_content"></View></p><p></LinearLayout>
是非Layout型對話方塊,直接使用AlertDialog
是使用了Layout的對話方塊,可以自訂控制項,實現更複雜的對話方塊
dialoglayout.xml的源碼:
<?xml version="1.0" encoding="utf-8"?></p><p><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent" android:layout_height="wrap_content"<br />android:orientation="vertical"><br /><EditText android:layout_height="wrap_content"<br />android:layout_width="fill_parent" android:layout_marginLeft="20dip"<br />android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/edtInput"/><br /></LinearLayout>
程式源碼:
package com.testAlertDialog;</p><p>import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.content.Context;<br />import android.content.DialogInterface;<br />import android.os.Bundle;<br />import android.view.Gravity;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.PopupWindow;</p><p>public class testAlertDialog extends Activity {<br />Button btnShowDialog;<br />Button btnShowDialog_Layout;<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> //定義按鈕<br /> btnShowDialog=(Button)this.findViewById(R.id.Button01);<br /> btnShowDialog.setOnClickListener(new ClickEvent());<br /> btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);<br /> btnShowDialog_Layout.setOnClickListener(new ClickEvent());<br /> }</p><p> //統一處理按鍵事件<br /> class ClickEvent implements OnClickListener{</p><p> @Override<br /> public void onClick(View v) {<br /> // TODO Auto-generated method stub<br /> if(v==btnShowDialog)<br /> showDialog(testAlertDialog.this);</p><p> else if(v==btnShowDialog_Layout)<br /> showDialog_Layout(testAlertDialog.this);</p><p> }</p><p> }</p><p> //顯示基本的AlertDialog<br />private void showDialog(Context context) {<br />AlertDialog.Builder builder = new AlertDialog.Builder(context);<br />builder.setIcon(R.drawable.icon);<br />builder.setTitle("Title");<br />builder.setMessage("Message");<br />builder.setPositiveButton("Button1",<br />new DialogInterface.OnClickListener() {<br />public void onClick(DialogInterface dialog, int whichButton) {<br />setTitle("點擊了對話方塊上的Button1");<br />}<br />});<br />builder.setNeutralButton("Button2",<br />new DialogInterface.OnClickListener() {<br />public void onClick(DialogInterface dialog, int whichButton) {<br />setTitle("點擊了對話方塊上的Button2");<br />}<br />});<br />builder.setNegativeButton("Button3",<br />new DialogInterface.OnClickListener() {<br />public void onClick(DialogInterface dialog, int whichButton) {<br />setTitle("點擊了對話方塊上的Button3");<br />}<br />});<br />builder.show();<br />}</p><p> //顯示基於Layout的AlertDialog<br />private void showDialog_Layout(Context context) {<br />LayoutInflater inflater = LayoutInflater.from(this);<br />final View textEntryView = inflater.inflate(<br />R.layout.dialoglayout, null);<br />final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);<br />final AlertDialog.Builder builder = new AlertDialog.Builder(context);<br />builder.setCancelable(false);<br />builder.setIcon(R.drawable.icon);<br />builder.setTitle("Title");<br />builder.setView(textEntryView);<br />builder.setPositiveButton("確認",<br />new DialogInterface.OnClickListener() {<br />public void onClick(DialogInterface dialog, int whichButton) {<br />setTitle(edtInput.getText());<br />}<br />});<br />builder.setNegativeButton("取消",<br />new DialogInterface.OnClickListener() {<br />public void onClick(DialogInterface dialog, int whichButton) {<br />setTitle("");<br />}<br />});<br />builder.show();<br />}<br />}<br />