剖析Android中 一个简单对话框

 一个简单讲述对话框的Android实例,剖析其中的原理,下文直接是代码。

***步:先配置项目中的res文件夹中values/strings.xml

Java代码:

 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3. <string name="app_name">普通对话框</string> 
  4. <string name="button">显示普通对话框</string> 
  5. <string name="title">普通对话框</string> 
  6. <string name="ok">确定</string> 
  7. <string name="message">这是一个普通对话框</string> 
  8. </resources> 

第二步:配置项目中的res文件夹中layout/main.xml 用来创建输入框和“显示普通对话框”按钮

java代码:

 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="wrap_content" 
  6. > 
  7. <EditText android:id="@+id/editText" 
  8. android:layout_width="fill_parent"   
  9. android:layout_height="wrap_content"   
  10. android:text="@string/message"/> 
  11. <Button android:id="@+id/button" 
  12. android:layout_width="fill_parent" 
  13. android:layout_height="wrap_content" 
  14. android:text="@string/button"/> 
  15. </LinearLayout> 

第三步:编写src文件夹包底下的.java文件

java代码:

 
 
 
  1. package eoe.demo;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. public class CommonDialogActivity extends Activity {  
  13. protected static final int COMMON_DIALOG = 1;  
  14. /** Called when the activity is first created. */  
  15. @Override  
  16. public void onCreate(Bundle savedInstanceState) {  
  17. super.onCreate(savedInstanceState);  
  18. setContentView(R.layout.main);  
  19. Button button= (Button)findViewById(R.id.button);  
  20. View.OnClickListener listener = new View.OnClickListener() {  
  21. public void onClick(View arg0) {  
  22. // TODO Auto-generated method stub  
  23. showDialog(COMMON_DIALOG);  
  24. }  
  25. };  
  26. button.setOnClickListener(listener);  
  27. }  
  28. /*   
  29. * 使用onCreateDialog(int)来管理对话框的状态,   
  30. * 那么每次对话框被解除时,   
  31. * 该对话框对象的状态会被Activity保存. 调用   
  32. * removeDialog(int)将所有该对象的内部引用   
  33. * 移除 如本程序那样,如果不加removeDialog,   
  34. * 那么显示的是***次的内容   
  35. */  
  36. protected Dialog onCreateDialog(int id){  
  37. AlertDialog dialog=null;  
  38. EditText editText = (EditText) findViewById(R.id.editText);  
  39. switch(id){  
  40. case COMMON_DIALOG:  
  41. Builder builder = new AlertDialog.Builder(this);  
  42. builder.setIcon(R.drawable.icon);  
  43. builder.setMessage(editText.getText());  
  44. builder.setTitle(R.string.title);  
  45. DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {  
  46. public void onClick(DialogInterface arg0, int arg1) {  
  47. // TODO Auto-generated method stub  
  48. removeDialog(COMMON_DIALOG);   
  49. }  
  50. };  
  51. builder.setPositiveButton(R.string.ok, listener);  
  52. dialog = builder.create();  
  53. break;  
  54. default:  
  55. break;  
  56. }  
  57. Log.e("onCreateDialog", "onCreateDialog");  
  58. return dialog;  
  59. }  

【编辑推荐】

Android布局属性详解

 Android 2.2系统的十个实用小技巧

谷歌Android UI设计技巧:优秀UI设计准则

谷歌Android UI设计技巧:图标与指导说明

Android实战系统对话框实现登陆注册功能

 

THE END