Andriod 對話框

在Activity中可以調用showDialog()來顯示一個對話框,覆蓋Activity的onCreateDialog方法,在這個方法中創建對話框,返回一個Dialog對象。

1.最簡單的對話框

  1. AlertDialog.Builder b=new  AlertDialog.Builder(this);  
  2. b.setTitle("簡單的");  
  3.             b.setMessage("this is a simple dialog");  
  4.             b.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  5.                   
  6.                 @Override  
  7.                 public void onClick(DialogInterface dialog, int which) {  
  8.                     // TODO Auto-generated method stub  
  9.                       
  10.                 }  
  11.             });  
  12.             b.setNegativeButton("否"new DialogInterface.OnClickListener() {  
  13.                   
  14.                 @Override  
  15.                 public void onClick(DialogInterface dialog, int which) {  
  16.                     // TODO Auto-generated method stub  
  17.                       
  18.                 }  
  19.             });  
  20.             return b.create();  
AlertDialog.Builder b=new  AlertDialog.Builder(this);
b.setTitle("簡單的");
			b.setMessage("this is a simple dialog");
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			b.setNegativeButton("否", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			return b.create();


效果如下

2.列表對話框

  1. b.setTitle("列表");  
  2.             //b.setMessage("message");這行代碼不要有  
  3.             b.setItems(items, new DialogInterface.OnClickListener() {  
  4.                   
  5.                 @Override  
  6.                 public void onClick(DialogInterface dialog, int which) {  
  7.                     // TODO Auto-generated method stub  
  8.                     Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();  
  9.                       
  10.                 }  
  11.             });  
  12.             return b.create();  
b.setTitle("列表");
			//b.setMessage("message");這行代碼不要有
			b.setItems(items, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
					
				}
			});
			return b.create();

items是一個String數組
效果圖

3.單選對話框

  1. b.setTitle("請選擇顏色");  
  2.             b.setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {  
  3.                   
  4.                 @Override  
  5.                 public void onClick(DialogInterface dialog, int which) {  
  6.                     // TODO Auto-generated method stub  
  7.                     Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();  
  8.                       
  9.                 }  
  10.             });  
  11.             b.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  12.                   
  13.                 @Override  
  14.                 public void onClick(DialogInterface dialog, int which) {  
  15.                     // TODO Auto-generated method stub  
  16.                       
  17.                 }  
  18.             });  
  19.             b.setNegativeButton("否"new DialogInterface.OnClickListener() {  
  20.                   
  21.                 @Override  
  22.                 public void onClick(DialogInterface dialog, int which) {  
  23.                     // TODO Auto-generated method stub  
  24.                       
  25.                 }  
  26.             });  
  27.             return b.create();  
  28.           
b.setTitle("請選擇顏色");
			b.setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
					
				}
			});
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			b.setNegativeButton("否", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			return b.create();
		

效果圖

4.多選對話框

 

  1. boolean []ddd=new boolean[3];  
  2.             b.setTitle("請選擇顏色");  
  3.             b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){  
  4.   
  5.                 @Override  
  6.                 public void onClick(DialogInterface dialog, int which,  
  7.                         boolean isChecked) {  
  8.                     // TODO Auto-generated method stub  
  9.                       
  10.                 }  
  11.                   
  12.             });  
  13.               
  14.             return b.create();  
boolean []ddd=new boolean[3];
			b.setTitle("請選擇顏色");
			b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					// TODO Auto-generated method stub
					
				}
				
			});
			
			return b.create();


效果圖


5.進度條對話框

  1. Handler hand=new Handler(){  
  2.   
  3.         @Override  
  4.         public void handleMessage(Message msg) {  
  5.             // TODO Auto-generated method stub  
  6.             super.handleMessage(msg);  
  7.             if(progressint>=100)  
  8.             {  
  9.                 pd.dismiss();  
  10.             }  
  11.             else  
  12.             {  
  13.                 progressint++;  
  14.                 pd.setProgress(progressint);  
  15.                 hand.sendEmptyMessageDelayed(0100);  
  16.             }  
  17.               
  18.         }  
  19.           
  20.     };  
  21. pd=new ProgressDialog(this);  
  22.             pd.setTitle("進度對話框");  
  23.             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  24.             pd.setMax(100);  
  25.             AndroidDialogActivity.this.hand.sendEmptyMessage(0);  
  26.             pd.setButton(DialogInterface.BUTTON_POSITIVE, "確定"new DialogInterface.OnClickListener(){  
  27.   
  28.                 @Override  
  29.                 public void onClick(DialogInterface dialog, int which) {  
  30.                     // TODO Auto-generated method stub  
  31.                       
  32.                       
  33.                 }});  
  34.             return pd;  
Handler hand=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(progressint>=100)
			{
				pd.dismiss();
			}
			else
			{
				progressint++;
				pd.setProgress(progressint);
				hand.sendEmptyMessageDelayed(0, 100);
			}
			
		}
		
	};
pd=new ProgressDialog(this);
			pd.setTitle("進度對話框");
			pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			pd.setMax(100);
			AndroidDialogActivity.this.hand.sendEmptyMessage(0);
			pd.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					
				}});
			return pd;


效果圖

6.代碼自定義對話框

  1. EditText et=new EditText(this);  
  2.             et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);  
  3.             b.setTitle("請輸入密碼");  
  4.             b.setView(et);  
  5.             return b.create();  
EditText et=new EditText(this);
			et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
			b.setTitle("請輸入密碼");
			b.setView(et);
			return b.create();


效果圖

7.XML文件自定義對話框

xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="用戶名" />  
  12.     <EditText  
  13.        android:id="@+id/EditText1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="TextView"  
  17.           
  18.         />  
  19.   
  20.     <TextView  
  21.         android:id="@+id/textView2"  
  22.          android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="密碼" />  
  25.   
  26.     <EditText  
  27.        android:id="@+id/EdiText2"  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="TextView"  
  31.         android:inputType="textPassword"  
  32.           
  33.         />  
  34.     <Button  
  35.         android:id="@+id/buttonyes"  
  36.          android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:text="確定" />  
  39.   
  40. </LinearLayout>  
<?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:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="用戶名" />
    <EditText
       android:id="@+id/EditText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        
        />

    <TextView
        android:id="@+id/textView2"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="密碼" />

    <EditText
       android:id="@+id/EdiText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:inputType="textPassword"
        
        />
    <Button
        android:id="@+id/buttonyes"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="確定" />

</LinearLayout>


Java代碼

  1. b.setIcon(R.drawable.ic_launcher);  
  2.             b.setTitle("自定義對話框");  
  3.             LayoutInflater li=LayoutInflater.from(this);  
  4.             View v=li.inflate(R.layout.info, null);  
  5.             Button yes=(Button) v.findViewById(R.id.buttonyes);  
  6.             yes.setOnClickListener(new OnClickListener(){  
  7.   
  8.                 @Override  
  9.                 public void onClick(View v) {  
  10.                     // TODO Auto-generated method stub  
  11.                     Toast.makeText(AndroidDialogActivity.this"Hello World", Toast.LENGTH_SHORT).show();  
  12.                 }});  
  13.             b.setView(v);  
  14.             return b.create();  
b.setIcon(R.drawable.ic_launcher);
			b.setTitle("自定義對話框");
			LayoutInflater li=LayoutInflater.from(this);
			View v=li.inflate(R.layout.info, null);
			Button yes=(Button) v.findViewById(R.id.buttonyes);
			yes.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
				}});
			b.setView(v);
			return b.create();


效果圖

 下載工程:下載

轉自:http://blog.csdn.net/zhy_cheng/article/details/8003467

轉自:http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html

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