Android入门之PopupWindow用法实例解析
程序员文章站
2024-01-23 11:51:34
本文实例介绍一下popupwindow对话框。popupwindow是阻塞对话框,只有在外部线程 或者 popupwindow本身做退出操作才可以执行。popupwindo...
本文实例介绍一下popupwindow对话框。popupwindow是阻塞对话框,只有在外部线程 或者 popupwindow本身做退出操作才可以执行。popupwindow完全依赖layout做外观,在常见的开发中,popupwindow应该会与alertdialog常混用。
先贴出本例中运行的结果图:
main.xml的源码如下:
<?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。
popupwindow.xml的源码如下:
<?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程序源码:
package com.testalertdialog; import android.app.activity; import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.text.editable; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.popupwindow; public class testalertdialog extends activity { button btnpopupwindow; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //定义按钮 btnpopupwindow=(button)this.findviewbyid(r.id.button01); btnpopupwindow.setonclicklistener(new clickevent()); } //统一处理按键事件 class clickevent implements onclicklistener{ @override public void onclick(view v) { // todo auto-generated method stub if(v==btnpopupwindow) { showpopupwindow(testalertdialog.this, testalertdialog.this.findviewbyid(r.id.button01)); } } } public void showpopupwindow(context context,view parent){ layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); final view vpopupwindow=inflater.inflate(r.layout.popupwindow, null, false); final popupwindow pw= new popupwindow(vpopupwindow,300,300,true); //ok按钮及其处理事件 button btnok=(button)vpopupwindow.findviewbyid(r.id.btnok); btnok.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { //设置文本框内容 edittext edtusername=(edittext)vpopupwindow.findviewbyid(r.id.username_edit); edtusername.settext("username"); edittext edtpassword=(edittext)vpopupwindow.findviewbyid(r.id.password_edit); edtpassword.settext("password"); } }); //cancel按钮及其处理事件 button btncancel=(button)vpopupwindow.findviewbyid(r.id.btncancel); btncancel.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { pw.dismiss();//关闭 } }); //显示popupwindow对话框 pw.showatlocation(parent, gravity.center, 0, 0); } }
推荐阅读
-
Android入门之Style与Theme用法实例解析
-
Android入门之PopupWindow用法实例解析
-
Android入门之AlertDialog用法实例分析
-
Android入门——数据解析之使用GSON解析JSON字符串(二)
-
Android开发之占位符应用实例解析
-
android开发之listView组件用法实例简析
-
Android编程四大组件之Activity用法实例分析
-
Android编程四大组件之BroadcastReceiver(广播接收者)用法实例
-
Android开发之浏览器用法实例详解(调用uc,opera,qq浏览器访问网页)
-
Android编程开发之RadioGroup用法实例