剖析Android中一个简单对话框

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

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

Java代码:

 
 
 
  1. 普通对话框
  2. 显示普通对话框
  3. 普通对话框
  4. 确定
  5. 这是一个普通对话框

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

java代码:

 
 
 
  1. android:orientation="vertical"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. >
  5. android:layout_width="fill_parent" 
  6. android:layout_height="wrap_content" 
  7. android:text="@string/message"/>
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/button"/>

第三步:编写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. }
  60. }

名称栏目:剖析Android中一个简单对话框
文章位置:http://www.hantingmc.com/qtweb/news2/318202.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联