Android入门第十篇之PopupWindow

  本文来自:http://blog.csdn.net/hellogv/article/details/5956358

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

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

Android入门第十篇之PopupWindow_第1张图片

main.xml的源码如下:

[xhtml] 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:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="PopupWindow演示"></Button>  
  8. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="PopupWindow演示"></Button> </LinearLayout>

 

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

 

Android入门第十篇之PopupWindow_第2张图片

popupwindow.xml的源码:

 

[xhtml] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  5.     android:orientation="vertical" android:background="#000000">  
  6.   
  7.     <TextView android:id="@+id/username_view"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_marginLeft="20dip"  
  10.         android:layout_marginRight="20dip" android:text="用户名"  
  11.         android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/>  
  12.   
  13.     <EditText android:id="@+id/username_edit"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_width="fill_parent" android:layout_marginLeft="20dip"  
  16.         android:layout_marginRight="20dip" android:capitalize="none"  
  17.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  18.   
  19.     <TextView android:id="@+id/password_view"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginLeft="20dip"  
  22.         android:layout_marginRight="20dip" android:text="密码"  
  23.         android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/>  
  24.   
  25.     <EditText android:id="@+id/password_edit"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_width="fill_parent" android:layout_marginLeft="20dip"  
  28.         android:layout_marginRight="20dip" android:capitalize="none"  
  29.         android:password="true"  
  30.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  31.   
  32. <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/BtnOK" android:layout_weight="100" android:text="确定"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="100" android:text="取消" android:id="@+id/BtnCancel"></Button></LinearLayout>  
  33. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#000000"> <TextView android:id="@+id/username_view" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="用户名" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> <EditText android:id="@+id/username_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password_view" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="密码" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> <EditText android:id="@+id/password_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/BtnOK" android:layout_weight="100" android:text="确定"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="100" android:text="取消" android:id="@+id/BtnCancel"></Button></LinearLayout> </LinearLayout>

 

接下来是程序源码:

[java] view plain copy print ?
  1. package com.testAlertDialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.text.Editable;  
  9. import android.view.Gravity;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.PopupWindow;  
  16.   
  17.   
  18. public class testAlertDialog extends Activity {  
  19.     Button btnPopupWindow;  
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         //定义按钮   
  26.         btnPopupWindow=(Button)this.findViewById(R.id.Button01);  
  27.         btnPopupWindow.setOnClickListener(new ClickEvent());  
  28.     }  
  29.       
  30.       
  31.     //统一处理按键事件   
  32.     class ClickEvent implements OnClickListener{  
  33.   
  34.         @Override  
  35.         public void onClick(View v) {  
  36.             // TODO Auto-generated method stub   
  37.             if(v==btnPopupWindow)  
  38.             {  
  39.                 showPopupWindow(testAlertDialog.this,  
  40.                         testAlertDialog.this.findViewById(R.id.Button01));  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45.     public void showPopupWindow(Context context,View parent){  
  46.         LayoutInflater inflater = (LayoutInflater)     
  47.            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
  48.         final View vPopupWindow=inflater.inflate(R.layout.popupwindow, nullfalse);  
  49.         final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);  
  50.   
  51.         //OK按钮及其处理事件   
  52.         Button btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);  
  53.         btnOK.setOnClickListener(new OnClickListener(){  
  54.             @Override  
  55.             public void onClick(View v) {  
  56.                 //设置文本框内容   
  57.                 EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);  
  58.                 edtUsername.setText("username");  
  59.                 EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);  
  60.                 edtPassword.setText("password");  
  61.             }  
  62.         });  
  63.           
  64.       //Cancel按钮及其处理事件   
  65.         Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);  
  66.         btnCancel.setOnClickListener(new OnClickListener(){  
  67.             @Override  
  68.             public void onClick(View v) {  
  69.                 pw.dismiss();//关闭   
  70.             }  
  71.         });  
  72.         //显示popupWindow对话框   
  73.         pw.showAtLocation(parent, Gravity.CENTER, 00);  
  74.     }  
  75.       
  76. }  

你可能感兴趣的:(android,XHTML,service,layout,button,encoding)