歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android對話框教程

1.1簡單提示框

[java]
  1. //簡單提示框   
  2.     private Dialog Dialog1(){  
  3.         return new AlertDialog.Builder(XXXActivity.this).setTitle("提示").setMessage("提示內容太短").show();  
  4.     }  

1.2多個按鈕提示框

[java]
  1. @Override  
  2.    protected Dialog onCreateDialog(int id) {  
  3.     if(id == 1){//兩個按鈕提示框   
  4.         return new AlertDialog.Builder(XXXActivity.this)  
  5.                         .setTitle("提示").setMessage("提示內容太短")  
  6.                         .setPositiveButton("確定"new DialogInterface.OnClickListener() {  
  7.                               
  8.                             @Override  
  9.                             public void onClick(DialogInterface dialog, int which) {  
  10.                                 // TODO Auto-generated method stub   
  11.                                 dismissDialog(1);  
  12.                             }  
  13.                         })  
  14.                         .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  15.                               
  16.                             @Override  
  17.                             public void onClick(DialogInterface dialog, int which) {  
  18.                                 // TODO Auto-generated method stub   
  19.                                 dismissDialog(1);  
  20.                             }  
  21.                         }).create();  
  22.     }else if(id == 2){//三個按鈕提示框   
  23.         return new AlertDialog.Builder(XXXActivity.this)  
  24.                     .setTitle("提示").setMessage("哈哈哈哈")  
  25.                     .setIcon(R.drawable.ic_launcher)  
  26.                     .setPositiveButton("注冊"new DialogInterface.OnClickListener() {  
  27.                           
  28.                         @Override  
  29.                         public void onClick(DialogInterface dialog, int which) {  
  30.                             // TODO Auto-generated method stub   
  31.                               
  32.                         }  
  33.                     })  
  34.                     .setNeutralButton("找回密碼"new DialogInterface.OnClickListener() {  
  35.                         //注意這裡和下面的取消是不一樣的setNeutral.....setNegative   
  36.                         @Override  
  37.                         public void onClick(DialogInterface dialog, int which) {  
  38.                             // TODO Auto-generated method stub   
  39.                               
  40.                         }  
  41.                     })  
  42.                     .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  43.                           
  44.                         @Override  
  45.                         public void onClick(DialogInterface dialog, int which) {  
  46.                             // TODO Auto-generated method stub   
  47.                               
  48.                         }  
  49.                     }).create();  
  50.     }else if(id ==4){ //圓形進度框   
  51.         ProgressDialog dialog =  new ProgressDialog(XXXActivity.this);  
  52.         dialog.setTitle("正在下載歌曲...");  
  53.         dialog.setMessage("請稍候......");  
  54.         return dialog;  
  55.     }  
  56.       
  57.     return super.onCreateDialog(id);  
  58.    }  
調用時showDialog(4);


1.3自定義對話框

命名xxxx.xml文件是我們想要自定義的風格

[html]
  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="wrap_content"  
  5.     android:orientation="vertical" >  
  6.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"  
  7.          android:layout_marginLeft="20dip" android:layout_marginRight="20dip"  
  8.          android:text="請輸入..." android:gravity="left"  
  9.          android:textAppearance="?android:attr/textAppearanceMedium"/>  
  10.     <EditText android:id="@+id/testdaysnum"  
  11.         android:layout_height="wrap_content"   
  12.         android:layout_width="fill_parent"  
  13.         android:layout_marginLeft="20dip"  
  14.         android:layout_marginRight="20dip"  
  15.         android:scrollHorizontally="true"  
  16.         android:autoText="false"  
  17.         android:capitalize="none"  
  18.         android:gravity="fill_horizontal"  
  19.         android:textAppearance="?android:attr/textAppearanceMedium"  
  20.         />  
  21.       
  22. </LinearLayout>  
Activity調用
[java]
  1. //自定義Dialog   
  2.        LayoutInflater factory = LayoutInflater.from(XXXActivity.this);  
  3.        final View testDialogView = factory.inflate(R.layout.xxxx, null);  
  4.        EditText edit_GetDayNum = (EditText)testDialogView.findViewById(R.id.testdaysnum);  
  5.        new AlertDialog.Builder(XXXActivity.this)  
  6.        .setTitle("提示")  
  7.        .setView(testDialogView)  
  8.        .setPositiveButton("確定"new DialogInterface.OnClickListener() {  
  9.           
  10.         @Override  
  11.         public void onClick(DialogInterface dialog, int which) {  
  12.             // TODO Auto-generated method stub   
  13.               
  14.             Toast.makeText(XXXActivity.this"請等待...", Toast.LENGTH_LONG).show();  
  15.         }  
  16.     }).create().show();  

1.4圓形進度框 [java]
  1. ProgressDialog m_Dialog = ProgressDialog.show(XXXActivity.this"請等待...""請稍候....",true);  

1.5Toast

[java]
  1. Toast.makeText(XXXActivity.this"請等待...", Toast.LENGTH_LONG).show();  
1.6模式對話框
[java]
  1. Dialog dialog = new Dialog(XXXActivity.this);  
  2.         Window window = dialog.getWindow();  
  3.         window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,  
  4.         WindowManager.LayoutParams.FLAG_BLUR_BEHIND);  
  5.         dialog.show();  
Copyright © Linux教程網 All Rights Reserved