下拉选择框,PopupWindow的使用
实现下拉选择框
直接上代码
Activity.java
package com.example.shaofei.customerviewdemo1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class ShowDownMenuDemoActivity extends AppCompatActivity {
private EditText mEt_input;
private ImageButton mIb_show_down;
private ArrayList<String> mListData;
private PopupWindow mPopupWindow;
private boolean isShow = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window_demo);
initView();
}
private void initView() {
mEt_input = (EditText) findViewById(R.id.et_input);
mIb_show_down = (ImageButton) findViewById(R.id.ib_show_down);
mIb_show_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isShow) {
mPopupWindow.dismiss();
isShow = false;
} else {
showPopupWindow();
isShow = true;
}
}
});
}
/**
* 显示PopupWindow
*/
private void showPopupWindow() {
ListView listView = new ListView(this);
mListData = new ArrayList<>();
for (int i = 0; i < 30; i++) {
mListData.add("10000" + i);
}
//设置数据适配器
listView.setAdapter(new MyAdapter());
listView.setDividerHeight(0);
mPopupWindow = new PopupWindow(listView, mEt_input.getWidth(), 800);
//设置点击空白处隐藏
mPopupWindow.setOutsideTouchable(true);
//在哪个位置,x偏移多少,y偏移多少
mPopupWindow.showAsDropDown(mEt_input, 0, 0);
//让Popupwindown可以获取焦点
mPopupWindow.setFocusable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mEt_input.setText(mListData.get(i));
mPopupWindow.dismiss();
isShow = false;
}
});
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return mListData.size();
}
@Override
public Object getItem(int i) {
return mListData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(viewGroup.getContext(), R.layout.item_popup, null);
holder.mTextView = view.findViewById(R.id.tv_center);
holder.mImageButton = view.findViewById(R.id.ib_delete);
view.setTag(holder);
}
holder = (ViewHolder) view.getTag();
holder.mTextView.setText(mListData.get(i));
holder.mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ShowDownMenuDemoActivity.this, "点击删除", Toast.LENGTH_SHORT).show();
mListData.remove(mListData.get(i));
notifyDataSetChanged();
if(mListData.size()==0) {
//如果删除到空集合,那么就隐藏
mPopupWindow.dismiss();
isShow = false;
}
}
});
return view;
}
}
class ViewHolder {
TextView mTextView;
ImageButton mImageButton;
}
}
xml文件:
<?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">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_gravity="center">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="点击输入"/>
<ImageButton
android:id="@+id/ib_show_down"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:background="@null"
android:src="@mipmap/arrow_down"/>
</RelativeLayout>
</LinearLayout>
ListView中的item布局:
<?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="50dp"
android:descendantFocusability="blocksDescendants"
android:layout_margin="20dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/arrow_constant"/>
<TextView
android:id="@+id/tv_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:gravity="center"
android:text="1000101"
android:textColor="#a000"/>
<ImageButton
android:id="@+id/ib_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@mipmap/arrow_delete"/>
</LinearLayout>
遇到的问题
PopupWindow中的ListView的item获取不到点击事件,原因是,item中的ImageButton抢占了item的焦点。我们可以在item的根布局中设置如下属性:
android:descendantFocusability=”blocksDescendants”
将其布局的焦点分块进行区分。就可以获取到焦点了。如果还是不能获取焦点的话,那么就在代码中再添加一行代码;
//让Popupwindown可以获取焦点
mPopupWindow.setFocusable(true);
下拉选择框,PopupWindow的使用的更多相关文章
- java、easyui-combotree树形下拉选择框
最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...
- FancySelect – 更好用的 jQuery 下拉选择框插件
FancySelect 这款插件是 Web 开发中下拉框功能的一个更好的选择.FancySelect 使用方便,只要绑定页面上的任何 Select 元素,并调用就 .fancySelect() 就可以 ...
- HTML、CSS小知识--兼容IE的下拉选择框select
HTML <div class="s_h_ie"> <select id="Select1" disabled="disabled& ...
- Bootstrap系列 -- 15. 下拉选择框select
Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple.Bootstrap框架会为这些元素提供统一的样式风格 <form role=&quo ...
- CSS自定义select下拉选择框(不用其他标签模拟)
今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...
- 基于jQuery美化联动下拉选择框
今天给大家介绍一款基于jQuery美化联动下拉选择框.这款下下拉选择框js里自带了全国所有城市的数数库.下拉选择框适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲 ...
- ul+jquery自定义下拉选择框
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框)
[源码下载] 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框) 作者:webabcd 介绍背水一战 Wind ...
- jQuery插件——下拉选择框
其实,之前也写过jQuery插件,今天写的是一个模拟select选择的下拉插件. 既然是jQuery插件,那么必然是依赖jQuery的了. 老规矩,直接上代码吧! ;(function () { $. ...
随机推荐
- Elasticsearch教程(一)简介与安装
简单概念 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...
- Qt中加载Libevent静态库(通过reimp和rs两条语句将lib转为a)
文章来源:http://blog.sina.com.cn/s/blog_731bf4c90102wnpr.html 本文仅是个人经验总结,若有错误欢迎指教! 最近要做一个跨平台的项目,同时也涉及到网络 ...
- QT_NO_CAST_FROM_ASCII这个宏的,禁用一切来自双引号字符串字面量传入QString(有2种解决方法)
这两天制作了两个Qt Creator增强套装的两个插件,其实也是非常简单的,但是其实花了我超过四天的时间,为什么呢?因为我之前很长一段时间都是在Linux下开发的,一切安好,没有任何问题,但是到了Wi ...
- 【canvas】基础练习三 图片
[canvas]Demo1 drawImage drawImage(img,x,y); <!DOCTYPE html> <html lang="en"> & ...
- asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发3-登录模块开发
进行本文之前需要在数据库用户表里面增加一条用户数据,直接手动添加即可,未安全考虑密码一定要使用Md5加密后的,这里提供666666的Md5密文为(c831b04de153469d),本文完成登录模块的 ...
- vue补充
一.安装vue-cli脚手架 1.淘宝镜像下载 用淘宝的国内服务器来向国外的服务器请求,我们向淘宝请求,而不是由我们直接向国外的服务器请求,会大大提升请求速度,使用时,将所有的npm命令换成cnpm即 ...
- Django之用户认证auth模块使用
Auth认证模块 执行数据库迁移的那两条命令时,即使我们没有建表,django是不是也会创建好多张表?我们创建之后去看一下里面的一个叫auth_user表,既然是表,那肯定应该有对应的操作改表的方法 ...
- 编译php扩展
在php编译安装好的情况下php扩展编译 php的很多模块都是以php的扩展形式来进行的.所以在php安装好的环境下需要用到之前安装时没有编译安装的php扩展的时候,这个时候编译安装php扩展就显得尤 ...
- Smobiler与Windows的异步回调差别
Smobiler与Windows的异步回调差别--基于.NET的APP开发和Windows开发差别 基于.NET的APP开发和Windows开发,异步回调差别 Windows app开发 异步回调 S ...
- mysql的union和or
实践出真知! 使用union连接 select `id` from `表名` where 0=0 and active=1 and `fullname` like '王%' union select ...