`
yidongkaifa
  • 浏览: 4063514 次
文章分类
社区版块
存档分类
最新评论

Android入门第十篇之PopupWindow .

 
阅读更多
介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。

贴出本例中运行的结果图:

main.xml的源码如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Buttonandroid:id="@+id/Button01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:text="PopupWindow演示"></Button>
  8. </LinearLayout>

下图是PopupWindow弹出的截图,这里的PopupWindow是个登录框,点“确定”则自动填写,点“取消”则关闭PopupWindow。

popupwindow.xml的源码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="wrap_content"
  4. android:orientation="vertical"android:background="#000000">
  5. <TextViewandroid:id="@+id/username_view"
  6. android:layout_height="wrap_content"
  7. android:layout_marginLeft="20dip"
  8. android:layout_marginRight="20dip"android:text="用户名"
  9. android:textAppearance="?android:attr/textAppearanceMedium"android:layout_width="fill_parent"/>
  10. <EditTextandroid:id="@+id/username_edit"
  11. android:layout_height="wrap_content"
  12. android:layout_width="fill_parent"android:layout_marginLeft="20dip"
  13. android:layout_marginRight="20dip"android:capitalize="none"
  14. android:textAppearance="?android:attr/textAppearanceMedium"/>
  15. <TextViewandroid:id="@+id/password_view"
  16. android:layout_height="wrap_content"
  17. android:layout_marginLeft="20dip"
  18. android:layout_marginRight="20dip"android:text="密码"
  19. android:textAppearance="?android:attr/textAppearanceMedium"android:layout_width="fill_parent"/>
  20. <EditTextandroid:id="@+id/password_edit"
  21. android:layout_height="wrap_content"
  22. android:layout_width="fill_parent"android:layout_marginLeft="20dip"
  23. android:layout_marginRight="20dip"android:capitalize="none"
  24. android:password="true"
  25. android:textAppearance="?android:attr/textAppearanceMedium"/>
  26. <LinearLayoutandroid:id="@+id/LinearLayout01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/BtnOK"android:layout_weight="100"android:text="确定"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="100"android:text="取消"android:id="@+id/BtnCancel"></Button></LinearLayout>
  27. </LinearLayout>

接下来是程序源码:

  1. packagecom.testAlertDialog;
  2. importandroid.app.Activity;
  3. importandroid.app.AlertDialog;
  4. importandroid.content.Context;
  5. importandroid.content.DialogInterface;
  6. importandroid.os.Bundle;
  7. importandroid.text.Editable;
  8. importandroid.view.Gravity;
  9. importandroid.view.LayoutInflater;
  10. importandroid.view.View;
  11. importandroid.view.View.OnClickListener;
  12. importandroid.widget.Button;
  13. importandroid.widget.EditText;
  14. importandroid.widget.PopupWindow;
  15. publicclasstestAlertDialogextendsActivity{
  16. ButtonbtnPopupWindow;
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. //定义按钮
  23. btnPopupWindow=(Button)this.findViewById(R.id.Button01);
  24. btnPopupWindow.setOnClickListener(newClickEvent());
  25. }
  26. //统一处理按键事件
  27. classClickEventimplementsOnClickListener{
  28. @Override
  29. publicvoidonClick(Viewv){
  30. //TODOAuto-generatedmethodstub
  31. if(v==btnPopupWindow)
  32. {
  33. showPopupWindow(testAlertDialog.this,
  34. testAlertDialog.this.findViewById(R.id.Button01));
  35. }
  36. }
  37. }
  38. publicvoidshowPopupWindow(Contextcontext,Viewparent){
  39. LayoutInflaterinflater=(LayoutInflater)
  40. context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  41. finalViewvPopupWindow=inflater.inflate(R.layout.popupwindow,null,false);
  42. finalPopupWindowpw=newPopupWindow(vPopupWindow,300,300,true);
  43. //OK按钮及其处理事件
  44. ButtonbtnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);
  45. btnOK.setOnClickListener(newOnClickListener(){
  46. @Override
  47. publicvoidonClick(Viewv){
  48. //设置文本框内容
  49. EditTextedtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);
  50. edtUsername.setText("username");
  51. EditTextedtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);
  52. edtPassword.setText("password");
  53. }
  54. });
  55. //Cancel按钮及其处理事件
  56. ButtonbtnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);
  57. btnCancel.setOnClickListener(newOnClickListener(){
  58. @Override
  59. publicvoidonClick(Viewv){
  60. pw.dismiss();//关闭
  61. }
  62. });
  63. //显示popupWindow对话框
  64. pw.showAtLocation(parent,Gravity.CENTER,0,0);
  65. }
  66. }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics