Android UI Custom Spinner drop-down box (implemented with Popuwindow)-Go

Source: Internet
Author: User

Define the layout of the first picture and the layout of the popup box (a ListView), here is not more said ~listview need to define a myspinneradapter~ do these preparations, is the implementation of the popup box ~

 protectedvoidonCreate(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 = newArrayList<String>();
09         list.add("第一展厅");
10         list.add("第二展厅");
11         list.add("第三展厅");
12         // 实例化一个适配器,list的数据作为Adapter的数据
13         adapter = newMyspinnerAdapter(this, list);
14         // 默认设置下拉框的标题为数据的第一个
15         textView.setText((CharSequence) adapter.getItem(0));
16         spinnerlayout = (LinearLayout) findViewById(R.id.spinnerid);
17         // 点击右侧按钮,弹出下拉框
18         imgView.setOnClickListener(newOnClickListener() {
19
20             @Override
21             publicvoidonClick(View v) {
22
23                 showWindow(v);
24
25             }
26         });
27     }
28
29     @SuppressWarnings("deprecation")
30     publicvoidshowWindow(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 = newPopupWindow(v);
39         // 设置弹框的宽度为布局文件的宽
40         popupWindow.setWidth(spinnerlayout.getWidth());
41         // 高度随着内容变化
42         popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
43         // 设置一个透明的背景,不然无法实现点击弹框外,弹框消失
44         popupWindow.setBackgroundDrawable(newBitmapDrawable());
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(newOnItemClickListener() {
55
56             @Override
57             publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2, longarg3) {
58                 // TODO Auto-generated method stub
59                 textView.setText(list.get(arg2));// 设置所选的item作为下拉框的标题
60                 // 弹框消失
61                 popupWindow.dismiss();
62                 popupWindow = null;
63             }
64         });

Spinner usage:

This.bank_name = (Spinner) Super.findviewbyid (r.id.spinnercitystatic);

This.bank_name.setOnItemSelectedListener (New Onitemselectedlistenerimpl ());

Drop-down box select event
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, "The City selected is:" + cities,
Toast.length_long). Show ();
}

@Override
public void onnothingselected (adapterview<?> parent) {
TODO auto-generated Method Stub
}

}

Android UI Custom Spinner drop-down box (implemented with Popuwindow)-Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.