安卓开发04:对话框例子

layout中设置两个可点击按钮:

[html]  view plain copy print ?
  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. <Button    
  8.     android:id="@+id/btn_dialog"    
  9.     android:layout_width="fill_parent"     
  10.     android:layout_height="wrap_content"    
  11.     android:text="@string/dialog"    
  12.     android:onClick="onClickDialog" />    
  13.       
  14. <Button    
  15.     android:id="@+id/btn_dialogs"    
  16.     android:layout_width="fill_parent"     
  17.     android:layout_height="wrap_content"    
  18.     android:text="@string/progressDialog"    
  19.     android:onClick="onClickProgressDialog" />    
  20.      
  21. </LinearLayout>   

Activity代码:

[java]  view plain copy print ?
  1. package com.example.androidtest;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.app.Dialog;  
  8. import android.app.ProgressDialog;  
  9. import android.content.DialogInterface;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.view.View;  
  14. import android.view.Window;  
  15. import android.widget.EditText;  
  16. import android.widget.Toast;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private String tag = "MainActivity";  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  26.         setContentView(R.layout.activity_main); //设置一个View模板  
  27.         Log.d(tag, "In the onCreate() event");  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onStart() {  
  32.         super.onStart();  
  33.         Log.d(tag, "In the onStart() event");  
  34.         Toast.makeText(getBaseContext(), "onStart", Toast.LENGTH_SHORT).show();  
  35.     }  
  36.   
  37.     public void onRestart() {  
  38.         super.onRestart();  
  39.         Log.d(tag, "In the onRestart() event");  
  40.     }  
  41.   
  42.     public void onResume() {  
  43.         super.onResume();  
  44.         Log.d(tag, "In the onResume() event");  
  45.     }  
  46.   
  47.     public void onPause() {  
  48.         super.onPause();  
  49.         Log.d(tag, "In the onPause() event");  
  50.     }  
  51.   
  52.     public void onStop() {  
  53.         super.onStop();  
  54.         Log.d(tag, "In the onStop() event");  
  55.     }  
  56.   
  57.     public void onDestroy() {  
  58.         super.onDestroy();  
  59.         Log.d(tag, "In the onDestroy() event");  
  60.     }  
  61.   
  62.     @Override  
  63.     public boolean onCreateOptionsMenu(Menu menu) {  
  64.         getMenuInflater().inflate(R.menu.main, menu);  
  65.         return true;  
  66.     }  
  67.       
  68.     /** 
  69.      * 点击打开对话框 
  70.      * @param v 
  71.      */  
  72.     @SuppressWarnings("deprecation")  
  73.     public void onClickDialog(View v) {  
  74.         Log.d(tag, "onClickDialog");  
  75.         showDialog(0);  
  76.     }  
  77.   
  78.     /** 
  79.      * 点击打开进度条 
  80.      * @param v 
  81.      */  
  82.     public void onClickProgressDialog(View v) {  
  83.         Log.d(tag, "onClickProgressDialog");  
  84.         final ProgressDialog progressDialog = new ProgressDialog(this);  
  85.         progressDialog.setTitle("ProgressDialog"); //设置标题  
  86.         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置样式  
  87.         progressDialog.setMessage("please wait...");  
  88.         progressDialog.setProgress(0); //设置进度开始点  
  89.         progressDialog.setMax(100); //进度最大是100  
  90.         progressDialog.show();  
  91.         new Thread(new Runnable() {  
  92.   
  93.             public void run() {  
  94.                 try {  
  95.                     Thread.sleep(1000);  
  96.                     progressDialog.setProgress(10); //每隔1秒就可以设置10  
  97.                     Thread.sleep(1000);  
  98.                     progressDialog.setProgress(20);  
  99.                     Thread.sleep(1000);  
  100.                     progressDialog.setProgress(30);  
  101.                     Thread.sleep(1000);  
  102.                     progressDialog.setProgress(40);  
  103.                     Thread.sleep(1000);  
  104.                     progressDialog.setProgress(50);  
  105.                     Thread.sleep(1000);  
  106.                     progressDialog.setProgress(60);  
  107.                     Thread.sleep(1000);  
  108.                     progressDialog.setProgress(70);  
  109.                     Thread.sleep(1000);  
  110.                     progressDialog.setProgress(90);  
  111.                     Thread.sleep(1000);  
  112.                     progressDialog.setProgress(100);  
  113.                     Thread.sleep(1000);  
  114.                     progressDialog.dismiss();  
  115.                 } catch (InterruptedException e) {  
  116.                     e.printStackTrace();  
  117.                 }  
  118.             }  
  119.         }).start();  
  120.     }  
  121.   
  122.     @Override  
  123.     protected Dialog onCreateDialog(int id) {  
  124.         switch (id) {  
  125.             case 0:  
  126.                 Builder builder = new AlertDialog.Builder(this); //创建一个对话框  
  127.                 builder.setTitle("Android 对话框"); //设置标题  
  128.                 builder.setMessage("message"); //设置message  
  129.                 builder.setIcon(android.R.drawable.ic_dialog_info); //设置图标  
  130.                 builder.setView(new EditText(this)); //设置一个填写的框  
  131.                 builder.setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, nullnull); //选择框  
  132.                 builder.setPositiveButton("确认"new DialogInterface.OnClickListener() {  
  133.                      
  134.                     public void onClick(DialogInterface dialog, int whichButton) {  
  135.                         Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show();  
  136.                     }  
  137.                 });  
  138.                 builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  139.   
  140.                     public void onClick(DialogInterface dialog, int whichButton) {  
  141.                         Toast.makeText(getBaseContext(), "NO!", Toast.LENGTH_SHORT).show();  
  142.                     }  
  143.                 });  
  144.                 return builder.create();  
  145.         }  
  146.         return null;  
  147.     }  
  148.   
  149. }  

普通对话框:

我们可以看到layout布局视图中有两个按钮,第一个按钮点击后打开的是普通对话框,点击事件android:onClick后调用Activity中的onClickDialog方法

[html]  view plain copy print ?
  1.       
  2. <Button    
  3.     android:id="@+id/btn_dialog"    
  4.     android:layout_width="fill_parent"     
  5.     android:layout_height="wrap_content"    
  6.     android:text="@string/dialog"    
  7.     android:onClick="onClickDialog" />    

Activity中,Dialog代码:

[java]  view plain copy print ?
  1. /** 
  2.  * 点击打开对话框 
  3.  * @param v 
  4.  */  
  5. @SuppressWarnings("deprecation")  
  6. public void onClickDialog(View v) {  
  7.     Log.d(tag, "onClickDialog");  
  8.     showDialog(0);  
  9. }  
  10.   
  11. /** 
  12.  * 需要重写创建Dialog方法 
  13.  */  
  14. @Override  
  15. protected Dialog onCreateDialog(int id) {  
  16.     switch (id) {  
  17.         case 0:  
  18.             Builder builder = new AlertDialog.Builder(this); //创建一个对话框  
  19.             builder.setTitle("Android 对话框"); //设置标题  
  20.             builder.setMessage("message"); //设置message  
  21.             builder.setIcon(android.R.drawable.ic_dialog_info); //设置图标  
  22.             builder.setView(new EditText(this)); //设置一个填写的框  
  23.             builder.setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, nullnull); //选择框  
  24.             builder.setPositiveButton("确认"new DialogInterface.OnClickListener() {  
  25.                  
  26.                 public void onClick(DialogInterface dialog, int whichButton) {  
  27.                     Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show();  
  28.                 }  
  29.             });  
  30.             builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  31.   
  32.                 public void onClick(DialogInterface dialog, int whichButton) {  
  33.                     Toast.makeText(getBaseContext(), "NO!", Toast.LENGTH_SHORT).show();  
  34.                 }  
  35.             });  
  36.             return builder.create();  
  37.     }  
  38.     return null;  
  39. }  


安卓开发04:对话框例子_第1张图片


进度对话框:

layout中第二个按钮就是调用进度对话框,进度对话框可以显示程序运行进度。

[html]  view plain copy print ?
  1. <Button    
  2.     android:id="@+id/btn_dialogs"    
  3.     android:layout_width="fill_parent"     
  4.     android:layout_height="wrap_content"    
  5.     android:text="@string/progressDialog"    
  6.     android:onClick="onClickProgressDialog" />   

Activity中,我们需要创建一个进度Dialog:

[java]  view plain copy print ?
  1. /** 
  2.    * 点击打开进度条 
  3.    * @param v 
  4.    */  
  5.   public void onClickProgressDialog(View v) {  
  6.       Log.d(tag, "onClickProgressDialog");  
  7.       final ProgressDialog progressDialog = new ProgressDialog(this);  
  8.       progressDialog.setTitle("ProgressDialog"); //设置标题  
  9.       progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置样式  
  10.       progressDialog.setMessage("please wait...");  
  11.       progressDialog.setProgress(0); //设置进度开始点  
  12.       progressDialog.setMax(100); //进度最大是100  
  13.       progressDialog.show();  
  14.       new Thread(new Runnable() {  
  15.   
  16.           public void run() {  
  17.               try {  
  18.                   //每1秒后,进度条显示10%  
  19.                   Thread.sleep(1000);  
  20.                   progressDialog.setProgress(10); //每隔1秒就可以设置10  
  21.                   Thread.sleep(1000);  
  22.                   progressDialog.setProgress(20);  
  23.                   Thread.sleep(1000);  
  24.                   progressDialog.setProgress(30);  
  25.                   Thread.sleep(1000);  
  26.                   progressDialog.setProgress(40);  
  27.                   Thread.sleep(1000);  
  28.                   progressDialog.setProgress(50);  
  29.                   Thread.sleep(1000);  
  30.                   progressDialog.setProgress(60);  
  31.                   Thread.sleep(1000);  
  32.                   progressDialog.setProgress(70);  
  33.                   Thread.sleep(1000);  
  34.                   progressDialog.setProgress(90);  
  35.                   Thread.sleep(1000);  
  36.                   progressDialog.setProgress(100);  
  37.                   Thread.sleep(1000);  
  38.                   progressDialog.dismiss();  
  39.               } catch (InterruptedException e) {  
  40.                   e.printStackTrace();  
  41.               }  
  42.           }  
  43.       }).start();  
  44.   }  


安卓开发04:对话框例子_第2张图片


消息模式弹出框:

[java]  view plain copy print ?
  1. Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show();   

也可以自定义:

[java]  view plain copy print ?
  1. Toast toast=Toast.makeText(getApplicationContext(), "显示带图片的toast"3000);   
  2.               toast.setGravity(Gravity.CENTER, 00);   
  3.               //创建图片视图对象   
  4.               ImageView imageView= new ImageView(getApplicationContext());   
  5.               //设置图片   
  6.               imageView.setImageResource(R.drawable.ic_launcher);   
  7.               //获得toast的布局   
  8.               LinearLayout toastView = (LinearLayout) toast.getView();   
  9.               //设置此布局为横向的   
  10.               toastView.setOrientation(LinearLayout.HORIZONTAL);   
  11.               //将ImageView在加入到此布局中的第一个位置   
  12.               toastView.addView(imageView, 0);   
  13.               toast.show();  

你可能感兴趣的:(Android开发)