定义出第一个图片的布局和弹出框(一个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); |
08 |
list = new ArrayList<String>(); |
13 |
adapter = new MyspinnerAdapter( this , list); |
15 |
textView.setText((CharSequence) adapter.getItem( 0 )); |
16 |
spinnerlayout = (LinearLayout) findViewById(R.id.spinnerid); |
18 |
imgView.setOnClickListener( new OnClickListener() { |
21 |
public void onClick(View v) { |
29 |
@SuppressWarnings ( "deprecation" ) |
30 |
public void showWindow(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 = new PopupWindow(v); |
40 |
popupWindow.setWidth(spinnerlayout.getWidth()); |
42 |
popupWindow.setHeight(LayoutParams.WRAP_CONTENT); |
44 |
popupWindow.setBackgroundDrawable( new BitmapDrawable()); |
46 |
popupWindow.setOutsideTouchable( true ); |
48 |
popupWindow.setFocusable( true ); |
50 |
popupWindow.setContentView(layout); |
52 |
popupWindow.showAsDropDown(v, -textView.getWidth(), 0 ); |
54 |
listView.setOnItemClickListener( new OnItemClickListener() { |
57 |
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { |
59 |
textView.setText(list.get(arg2)); |
61 |
popupWindow.dismiss(); |
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
}
}