Android入门第十篇之PopupWindow
来源:互联网 发布:手机打开docx软件 编辑:程序博客网 时间:2024/05/01 20:31
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!
介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
贴出本例中运行的结果图:
main.xml的源码如下:
下图是PopupWindow弹出的截图,这里的PopupWindow是个登录框,点“确定”则自动填写,点“取消”则关闭PopupWindow。
popupwindow.xml的源码:
接下来是程序源码:
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入门第十篇之PopupWindow
- Android入门第十篇之PopupWindow
- Android入门第十篇之PopupWindow
- Android入门第十篇之PopupWindow
- Android入门第十篇之PopupWindow .
- Android入门第十篇之PopupWindow
- Android入门第十篇之PopupWindow
- Android入门/PopupWindow(十一)
- Android入门第十一篇之TabHost,TabWidget
- Android入门第十一篇之TabHost,TabWidget
- Android入门第十一篇之TabHost,TabWidget
- Android入门第十一篇之TabHost,TabWidget .
- Android入门第十一篇之TabHost,TabWidget
- 第十章,PopupWindow简易demo(Android)
- Android开发之PopupWindow
- Android之PopupWindow
- android之PopupWindow
- android控件之popupWindow
- JBPM之流程执行的基本概念
- java的4种reference: strong, soft, weak, phantom
- kernel-2.6.38内核驱动globalmem--添加中断机制和udev机制
- 分享国外WP7开源控件库
- Oracle 游标(cursor) 说明
- Android入门第十篇之PopupWindow
- C#中的托管代码与非托管代码
- 用mtrace检查内存泄漏
- U-BOOT分析(smdk2410.c)
- java加密解密
- 一段复合查询的sql语句备份 排序 group
- android面试题目及其答案
- 函数调用过程中堆栈运行情况
- UVALive4922_Queen Collision