介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
贴出本例中运行的结果图:
main.xml的源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<Buttonandroid:id="@+id/Button01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:text="PopupWindow演示"></Button>
-
</LinearLayout>
下图是PopupWindow弹出的截图,这里的PopupWindow是个登录框,点“确定”则自动填写,点“取消”则关闭PopupWindow。
popupwindow.xml的源码:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"android:layout_height="wrap_content"
-
android:orientation="vertical"android:background="#000000">
-
<TextViewandroid: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"/>
-
<EditTextandroid: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"/>
-
<TextViewandroid: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"/>
-
<EditTextandroid: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"/>
-
<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>
-
</LinearLayout>
接下来是程序源码:
- packagecom.testAlertDialog;
-
importandroid.app.Activity;
-
importandroid.app.AlertDialog;
-
importandroid.content.Context;
-
importandroid.content.DialogInterface;
-
importandroid.os.Bundle;
-
importandroid.text.Editable;
-
importandroid.view.Gravity;
-
importandroid.view.LayoutInflater;
-
importandroid.view.View;
-
importandroid.view.View.OnClickListener;
-
importandroid.widget.Button;
-
importandroid.widget.EditText;
-
importandroid.widget.PopupWindow;
-
publicclasstestAlertDialogextendsActivity{
- ButtonbtnPopupWindow;
-
-
@Override
-
publicvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
btnPopupWindow=(Button)this.findViewById(R.id.Button01);
-
btnPopupWindow.setOnClickListener(newClickEvent());
- }
-
-
classClickEventimplementsOnClickListener{
-
@Override
-
publicvoidonClick(Viewv){
-
-
if(v==btnPopupWindow)
- {
-
showPopupWindow(testAlertDialog.this,
-
testAlertDialog.this.findViewById(R.id.Button01));
- }
- }
- }
-
publicvoidshowPopupWindow(Contextcontext,Viewparent){
- LayoutInflaterinflater=(LayoutInflater)
- context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
finalViewvPopupWindow=inflater.inflate(R.layout.popupwindow,null,false);
-
finalPopupWindowpw=newPopupWindow(vPopupWindow,300,300,true);
-
- ButtonbtnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);
-
btnOK.setOnClickListener(newOnClickListener(){
-
@Override
-
publicvoidonClick(Viewv){
-
- EditTextedtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);
-
edtUsername.setText("username");
- EditTextedtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);
-
edtPassword.setText("password");
- }
- });
-
- ButtonbtnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);
-
btnCancel.setOnClickListener(newOnClickListener(){
-
@Override
-
publicvoidonClick(Viewv){
-
pw.dismiss();
- }
- });
-
-
pw.showAtLocation(parent,Gravity.CENTER,0,0);
- }
- }
分享到:
相关推荐
Android入门第十篇之PopupWindow.docx
介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程或者PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该...
Android项目实现半透明的popupwindow.rar
安卓Android源码——仿微信popupwindow.zip
仿微信右上角popupwindow的实现,之前网上找了很多,但总是出现测试的时候popupwindow的显示位置不是自己想要的右上角
Android高级应用源码-仿微信popupwindow.zip
Android应用源码实现半透明的popupwindow.zip项目安卓应用源码下载Android应用源码实现半透明的popupwindow.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术...
安卓Android源码——实现半透明的popupwindow.zip
适合学习Android的同学和基于毕业生学习和参考,Android studio、idea、eclipse可以运行,建议使用Android studio或者idea
仿微信popupwindow.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android应用源码之仿微信标题栏右上角PopupWindow.zip
安卓Andriod源码——仿微信popupwindow.zip
时隔一年,又要准备做Android的开发了,最近复习和整理一下Android的知识。这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是...
Android应用源码之仿微信标题栏右上角PopupWindow.zip项目安卓应用源码下载Android应用源码之仿微信标题栏右上角PopupWindow.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合...
仿微信popupwindow.rar
Android弹窗PopupWindow,实现代码