如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题-FenGKun
来源:互联网 发布:局域网办公软件 编辑:程序博客网 时间:2024/05/01 16:31
在android中有时候可能要实现一个底部弹出菜单,此时可以考虑用PopupWindow来实现。下面就来介绍一下如何使用PopupWindow实现一个弹出窗。
主Activity代码:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //得到弹出菜单的view,login_setting_popup是弹出菜单的布局文件 View view = getLayoutInflater().inflate(R.layout.login_setting_popup, null); //初始化弹出菜单 popWindow = new PopupWindow(view, WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.WRAP_CONTENT,false); //设置可以获取焦点,否则弹出菜单中的EditText是无法获取输入的 popWindow.setFocusable(true); //这句是为了防止弹出菜单获取焦点之后,点击activity的其他组件没有响应 popWindow.setBackgroundDrawable(new BitmapDrawable()); //防止虚拟软键盘被弹出菜单遮住 popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); //在底部显示 popWindow.showAtLocation(this,Gravity.BOTTOM, 0, 0); }
</pre><p><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.63636302947998px; line-height:25.454544067382813px"><span style="color:#ff6666">login_setting_popup.xml:</span></span></p><p><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.63636302947998px; line-height:25.454544067382813px"></span></p><p><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:13.63636302947998px; line-height:25.454544067382813px"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#eeeeee"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="20dp" android:layout_marginRight="30dp"> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.2" android:textSize="12sp" android:gravity="center_vertical" android:textColor="@android:color/black" android:text="服务器地址"> </TextView> <EditText android:id="@+id/login_setting_adderss_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.8"> </EditText> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="20dp" android:layout_marginRight="30dp"> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.2" android:textSize="12sp" android:textColor="@android:color/black" android:gravity="center_vertical" android:text="GPS"> </TextView> <RadioGroup android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="0.8"> <RadioButton android:id="@+id/open_gps_rb" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="12sp" android:textColor="@android:color/black" android:checked="true" android:text="打开"> </RadioButton> <RadioButton android:id="@+id/close_gps_rb" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="12sp" android:layout_marginLeft="20dp" android:textColor="@android:color/black" android:text="关闭"> </RadioButton> </RadioGroup> </LinearLayout></LinearLayout>
这样就可以实现一个底部弹出菜单了。但是在用PopupWindow实现底部弹出菜单的时候要注意几个问题:
1)如果弹出菜单中有EditText这种输入控件,如果不给PopupWindow设置可获取焦点的话,EditText是无法获取输入的。
2)一定要设置 虚拟软键盘随需要更改屏幕显示内容的大小,否则虚拟软键盘会被底部弹出菜单遮挡住。
3)如果设置了PopupWindow可获取焦点的话,此时会遇到一个问题就是当PopupWindow中的控件比如EditText获取焦点之后,点击PopupWindow之外的控件是不会有响应的,如果用setBackgroundDrawable(new BitmapDrawable())进行设置的话,则不会出现这种情况。
0 0
- 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题-FenGKun
- 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题
- 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题
- EditText获取焦点并弹出软键盘
- edittext获取焦点并弹出软键盘
- popupwindow中EditText获取焦点后自动弹出软键盘
- popupwindow中EditText获取焦点后自动弹出软键盘
- popupwindow中EditText获取焦点后自动弹出软键盘
- popupwindow中EditText获取焦点后自动弹出软键盘
- popupwindow中EditText获取焦点后自动弹出软键盘
- Popupwindow里用EditText获取焦点弹出软键盘的问题
- 替换PopUpWindow(解决焦点获取问题)实现顶部筛选菜单(顶部不变,底部阴暗)
- android EditText 自动获取焦点并弹出软键盘
- Android EditText获取焦点并弹出软键盘
- Android 设置Edittext获取焦点并弹出软键盘
- Android EditText获取焦点并弹出软键盘
- 移动端解决fixed和input获取焦点软键盘弹出影响定位的问题
- 解决刚进入APP时搜索框获取焦点,软键盘自动弹出的问题
- Linux下实现秒级定时任务的两种方案(crontab 每秒运行)
- 我的第一个OC语言程序
- java list add addall
- TextView文本长度超出屏幕(ellipsize属性)
- 透明色设置
- 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题-FenGKun
- AT指令中的<CR>和<LF>分别代表什么意思
- ORA-00600: 内部错误代码, 参数: [kcratr1_lastbwr]
- 使用自定义 OpenSSL 库编译 nginx
- 短信计费
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- java 理解static代码块的作用
- as3遍历当前显示对象
- 说话的艺术