有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。

1.创建一个名为Dialog的工程。

2.main.xml中的代码。

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <Button
  7. android:id="@+id/btn_dialog"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Clicktodisplayadialog"
  11. android:onClick="onClick"/>
  12. </LinearLayout>

3.DialogActivity.java中的代码。

[java] view plain copy
  1. packagenet.horsttnann.Dialog;
  2. importandroid.app.Activity;
  3. importandroid.app.AlertDialog;
  4. importandroid.app.Dialog;
  5. importandroid.content.DialogInterface;
  6. importandroid.os.Bundle;
  7. importandroid.view.View;
  8. importandroid.widget.Toast;
  9. publicclassDialogActivityextendsActivity{
  10. CharSequence[]items={"Google","Apple","Microsoft"};
  11. boolean[]itemsChecked=newboolean[items.length];
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. }
  18. publicvoidonClick(Viewv){
  19. showDialog(0);
  20. }
  21. @Override
  22. protectedDialogonCreateDialog(intid){
  23. switch(id){
  24. case0:
  25. returnnewAlertDialog.Builder(this)
  26. .setIcon(R.drawable.ic_launcher)
  27. .setTitle("Thisisadialogwithsomesimpletext...")
  28. .setPositiveButton("OK",
  29. newDialogInterface.OnClickListener(){
  30. publicvoidonClick(DialogInterfacedialog,
  31. intwhichButton){
  32. Toast.makeText(getBaseContext(),
  33. "OKclicked!",Toast.LENGTH_SHORT)
  34. .show();
  35. }
  36. })
  37. .setNegativeButton("Cancel",
  38. newDialogInterface.OnClickListener(){
  39. publicvoidonClick(DialogInterfacedialog,
  40. intwhichButton){
  41. Toast.makeText(getBaseContext(),
  42. "Cancelclicked!",
  43. Toast.LENGTH_SHORT).show();
  44. }
  45. })
  46. .setMultiChoiceItems(items,itemsChecked,
  47. newDialogInterface.OnMultiChoiceClickListener(){
  48. publicvoidonClick(DialogInterfacedialog,
  49. intwhich,booleanisChecked){
  50. Toast.makeText(
  51. getBaseContext(),
  52. items[which]
  53. +(isChecked?"checked!"
  54. :"unchecked!"),
  55. Toast.LENGTH_SHORT).show();
  56. }
  57. }).create();
  58. }
  59. returnnull;
  60. }
  61. }

4.调试。

点击按钮弹出对话框,在CheckBox上面打勾,就会弹出一个Toast提示,显示选中物件的文本信息。点击“OK”或“Cancel”按钮会使对话框消失。

效果图:

更多信息请查看java进阶网http://www.javady.com

更多相关文章

  1. Android(安卓)滑动效果进阶篇(五)—— 3D旋转
  2. Android进阶(二)https请求No peer certificate的解决方法.
  3. Android(安卓)SDK 2.3与Eclipse最新版开发环境搭建(五)
  4. Android(安卓)中访问网页 webView
  5. 颜色选择器【ColorPickerPreference】自定义Preference 对话框
  6. Android(安卓)WindowManager悬浮窗
  7. Android(安卓)自定义弹出对话框实例
  8. [置顶] 我的Android进阶之旅------>Android中查看应用签名信息
  9. mono for android读书笔记之真机调试

随机推荐

  1. Android中获取正在运行的应用程序-----Ac
  2. ScaleGestureDetector SimpleOnScaleGest
  3. 【转载】Using SQLite from Shell in And
  4. Eclipse 开发 Android, Hello, DatePicke
  5. 解决com.google.code.findbugs:jsr305依
  6. Unable to execute dex: java.nio.Buffer
  7. android lowmem killer 触发条件
  8. C# and android
  9. AndroidFragment切换
  10. android jetpack Navigation 获取当前激