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); |
08 |
list = newArrayList<String>(); |
12 |
// 实例化一个适配器,list的数据作为Adapter的数据 |
13 |
adapter = newMyspinnerAdapter(this, list); |
15 |
textView.setText((CharSequence) adapter.getItem(0)); |
16 |
spinnerlayout = (LinearLayout) findViewById(R.id.spinnerid); |
18 |
imgView.setOnClickListener(newOnClickListener() { |
21 |
publicvoidonClick(View v) { |
29 |
@SuppressWarnings("deprecation") |
30 |
publicvoidshowWindow(View v) { |
32 |
layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.mypinner_dropdown, null); |
34 |
listView = (ListView) layout.findViewById(R.id.listView); |
36 |
listView.setAdapter(adapter); |
38 |
popupWindow = newPopupWindow(v); |
40 |
popupWindow.setWidth(spinnerlayout.getWidth()); |
42 |
popupWindow.setHeight(LayoutParams.WRAP_CONTENT); |
43 |
// 设置一个透明的背景,不然无法实现点击弹框外,弹框消失 |
44 |
popupWindow.setBackgroundDrawable(newBitmapDrawable()); |
46 |
popupWindow.setOutsideTouchable(true); |
48 |
popupWindow.setFocusable(true); |
50 |
popupWindow.setContentView(layout); |
51 |
// 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移 |
52 |
popupWindow.showAsDropDown(v, -textView.getWidth(), 0); |
54 |
listView.setOnItemClickListener(newOnItemClickListener() { |
57 |
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2, longarg3) { |
58 |
// TODO Auto-generated method stub |
59 |
textView.setText(list.get(arg2));// 设置所选的item作为下拉框的标题 |
61 |
popupWindow.dismiss(); |
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