Android UI自定义Spinner下拉框(用popuwindow实现)-转

定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~

 protected void onCreate(Bundle savedInstanceState) {
03         super.onCreate(savedInstanceState);
04         setContentView(R.layout.activity_main);
05         textView = (TextView) findViewById(R.id.text);
06         imgView = findViewById(R.id.arrowbut);
07         // 实例化一个List,添加数据
08         list = new ArrayList<String>();
09         list.add("第一展厅");
10         list.add("第二展厅");
11         list.add("第三展厅");
12         // 实例化一个适配器,list的数据作为Adapter的数据
13         adapter = new MyspinnerAdapter(this, list);
14         // 默认设置下拉框的标题为数据的第一个
15         textView.setText((CharSequence) adapter.getItem(0));
16         spinnerlayout = (LinearLayout) findViewById(R.id.spinnerid);
17         // 点击右侧按钮,弹出下拉框
18         imgView.setOnClickListener(new OnClickListener() {
19  
20             @Override
21             public void onClick(View v) {
22  
23                 showWindow(v);
24  
25             }
26         });
27     }
28  
29     @SuppressWarnings("deprecation")
30     public void showWindow(View v) {
31         // 找到布局文件
32         layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.mypinner_dropdown, null);
33         // 实例化listView
34         listView = (ListView) layout.findViewById(R.id.listView);
35         // 设置listView的适配器
36         listView.setAdapter(adapter);
37         // 实例化一个PopuWindow对象
38         popupWindow = new PopupWindow(v);
39         // 设置弹框的宽度为布局文件的宽
40         popupWindow.setWidth(spinnerlayout.getWidth());
41         // 高度随着内容变化
42         popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
43         // 设置一个透明的背景,不然无法实现点击弹框外,弹框消失
44         popupWindow.setBackgroundDrawable(new BitmapDrawable());
45         // 设置点击弹框外部,弹框消失
46         popupWindow.setOutsideTouchable(true);
47         // 设置焦点
48         popupWindow.setFocusable(true);
49         // 设置所在布局
50         popupWindow.setContentView(layout);
51         // 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移
52         popupWindow.showAsDropDown(v, -textView.getWidth(), 0);
53         // listView的item点击事件
54         listView.setOnItemClickListener(new OnItemClickListener() {
55  
56             @Override
57             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
58                 // TODO Auto-generated method stub
59                 textView.setText(list.get(arg2));// 设置所选的item作为下拉框的标题
60                 // 弹框消失
61                 popupWindow.dismiss();
62                 popupWindow = null;
63             }
64         });

spinner用法:

this.bank_name = (Spinner) super.findViewById(R.id.spinnerCityStatic);

this.bank_name.setOnItemSelectedListener(new OnItemSelectedListenerImpl());

// 下拉框选择事件
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String city = parent.getItemAtPosition(position).toString();
Toast.makeText(BangBankActivity.this, "选择的城市是:" + city,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}

}

时间: 2024-08-04 23:31:59

Android UI自定义Spinner下拉框(用popuwindow实现)-转的相关文章

spinner下拉框组件

方法一代码如下: <string-array name="city_name"> <item>浙江</item> <item>上海</item> <item>北京</item> </string-array> <Spinner android:id="@+id/citySelect" android:layout_width="fill_parent&qu

Android 可以输入的下拉框

将AutoCompleteTextView与一个button组合起来,做成一个可以输入,也可以选择的下拉框. 下面是定义的xml文件,auto_spinner.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools=&

自定义SWT控件一之自定义单选下拉框

一.自定义下拉控件 自定义的下拉框,是自定义样式的,其中的下拉框使用的是独立的window,非复选框的下拉框双击单机其它区域或选择完之后,独立window构成的下拉框会自动消失. package com.view.control.select; import java.util.ArrayList; import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite;

android:Spinner(下拉框)控件的使用

1.效果图 2.创建页面文件(main.xml) 1 <Spinner 2 android:id="@+id/spinner1" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" /> 5 6 <TextView 7 android:id="@+id/tvResult" 8 android:layout_w

Android自学笔记之Spinner下拉框的功能、使用

Spinner: 是类似下拉列表的一种控件,用户从中选择相应选项! Spinner中的数据需要使用Adapter(适配器)填充. 使用: 布局文件activity_main.xml: <span style="font-family:KaiTi_GB2312;font-size:18px;"><Spinner android:id="@+id/spinner" android:layout_width="match_parent"

Android实现三级联动下拉框 下拉列表spinner的实例

主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_

android 开发-spinner下拉框控件的实现

Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textView

Bootstrap Flat UI的select下拉框显示问题解决

bootstrap中文网(http://www.bootcss.com/)里推荐项目有Flat UI(http://www.bootcss.com/p/flat-ui/),非常漂亮,而且少有的竟然具有select下拉框. 我直接打开example(Flat-UI-master/docs/examples/select.html)都是可以的,但是放到我的网站里则不行. 使用chrome调试经过分析,原因是: $('[data-toggle="select"]').select2(); 这

Android基础——高级UI组件:下拉框,列表,滚动条视图

布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.andr