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"> <TextView android:id="@+id/statusinfo" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="当前用户状态:在线" /> <Button android:id="@+id/popbut" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="状态" /> </LinearLayout>
popwindow.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/popinfo" android:text="请选择您的当前状态:" android:layout_width="wrap_content" android:textSize="20px" android:layout_height="wrap_content"/> <RadioGroup android:id="@+id/changestatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:checkedButton="@+id/online"> <RadioButton android:id="@+id/online" android:text="在线"/> <RadioButton android:id="@+id/offline" android:text="离线"/> <RadioButton android:id="@+id/stealth" android:text="隐身"/> </RadioGroup> <Button android:id="@+id/cancel" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="取消" /> </LinearLayout>
MyPopupWindowDemo.java:
import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.PopupWindow; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class MyPopupWindowDemo extends Activity { private Button popbut = null; // 按钮组件 private RadioGroup changestatus = null; // 单选钮组件 private TextView statusinfo = null; // 文本显示组件 private Button cancel = null; // 按钮组件 private PopupWindow popWin = null; // 弹出窗口 private View popView = null; // 保存弹出窗口布局 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 设置布局管理器 this.popbut = (Button) super.findViewById(R.id.popbut); // 取得组件 this.statusinfo = (TextView) super.findViewById(R.id.statusinfo); // 取得组件 this.popbut.setOnClickListener(new OnClickListenerImpl()); // 设置单击事件 } private class OnClickListenerImpl implements OnClickListener { // 设置监听 @Override public void onClick(View view) { LayoutInflater inflater = LayoutInflater .from(MyPopupWindowDemo.this); // 取得LayoutInflater对象 MyPopupWindowDemo.this.popView = inflater.inflate( R.layout.popwindow, null); // 读取布局管理器 MyPopupWindowDemo.this.popWin = new PopupWindow(popView, 300, 220, true); // 实例化PopupWindow MyPopupWindowDemo.this.changestatus = (RadioGroup) popView .findViewById(R.id.changestatus); // 取得组件 MyPopupWindowDemo.this.cancel = (Button) popView .findViewById(R.id.cancel); // 取得组件 MyPopupWindowDemo.this.changestatus .setOnCheckedChangeListener( new OnCheckedChangeListenerImpl()); // 设置监听 MyPopupWindowDemo.this.cancel .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyPopupWindowDemo.this.popWin.dismiss(); // 关闭弹出窗口 } }); MyPopupWindowDemo.this.popWin.showAtLocation( MyPopupWindowDemo.this.popbut, Gravity.CENTER, 0, 0); // 显示弹出窗口 } } private class OnCheckedChangeListenerImpl implements OnCheckedChangeListener { // 选项选中时触发 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton but = (RadioButton) MyPopupWindowDemo.this.popView .findViewById(group.getCheckedRadioButtonId()); // 取得选中组件 MyPopupWindowDemo.this.statusinfo.setText("当前用户状态:" + but.getText().toString()); // 设置文本 MyPopupWindowDemo.this.popWin.dismiss(); // 关闭弹出窗口 } } }
相关推荐
AS写的PopupWindow demo blog:http://blog.csdn.net/shenyuanqing/article/details/48136639
分享一个在android开发中的多级PopupWindow 弹出窗口实例,不知道这种多级的弹出窗口算不算是“嵌套”方式?因为它们之间确实是有着层级的关系,当PopupWindow被主界面按钮触发的时候,会被弹出第一级,在第一级中...
大家可自行学习,研究,并补充该demo.
本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下: 布局文件: 主布局文件:activity_main: <?xml version=1.0 encoding=utf-8?> <...
Android评论PopupWindow弹出窗,可根据输入法自适应高度,点击输入框时,输入法弹出和隐藏,对于我们的评论窗口而言,高度始终都是保持不变,这样可以给用户带来更好的体验! 具体实现代码地址:...
(1)Popupwindow在显示之前一定要设置宽高,Dialog无此限制。 (2)Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。 (3)Popupwindow不会...
主要为大家详细介绍了Android Popupwindow弹出窗口的简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
PopupWindow弹出窗口案例源码,最近做一个项目用到了popupWindow,遇到一个问题,当PopupWindow向下弹出时的界面空间不够用时,会自动向上弹出。如何分别设置向下弹出以及向上弹出的背景呢。
主要介绍了Android自定义弹出窗口PopupWindow使用技巧,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
安卓 Android PopupWindow浮动窗口实现实例,或者说是弹出窗口吧,总之就是地一类,在打开应用后,显示在最上层的一个浮动窗口效果,Andorid弹出窗口,Google为Andorid预置了几款默认的弹出窗口效果,本源码教你如何...
目前只见过两种弹出框的实现方式,第一种是最常见的PopupWindow,第二种也就是Activity的方式是前几天才见识过。感觉很霸气哦。没想到,activity也可以做伪窗口。 先贴上最常见的方法,主要讲activity的方法。 一、...
Android开发中常用的弹出式窗口,包含平移特效。
自定义单选、多选对话框及popupwindow窗口实例源码是一个自定义的弹出对话框例子源码,弹出的时候有半透明效果,提供的有弹出单选(popwindow)、弹出多选(popwindow)、弹出单选(dialog)、弹出多选(dialog)等几种实现...
点击从底部弹出菜单,覆盖在当前Activity上面,背景灰色透明
PopupWindow,弹出框使用PopupWindow实现,这是点赞和评论的载体,具体要涉及 PopupWindow 点击非窗口位置和再次点击消失以及显示位置的问题(根据相应更多按钮的位置确定 PopupWindow 的显示位置 package ...
主要介绍了Android开发中PopupWindow用法,结合实例形式分析了PopupWindow弹出窗口效果的使用技巧,需要的朋友可以参考下