Android入门之PopupWindow用法实例解析

所属分类: 软件编程 / Android 阅读数: 1274
收藏 0 赞 0 分享

本文实例介绍一下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);
 }
}
更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多