本文实例讲述了Android编程实现多列显示的下拉列表框Spinner功能。分享给大家供大家参考,具体如下:
扶绥ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
先上张效果图:
严格来说,这并不是一个下拉列表,只不过实现的效果很像是下拉列表
实现原理:
该下拉列表其实是一个AlertDialog.Builder,给它设置了一个以LinearLayout布局的View。该View内使用了一个GridView组件,
代码:
package yyy.testandroid9; private Intent intent; private int index = 0; private SharedPreferences preferences; private File file; private Spinner spinner; private GridView gridView; private LinearLayout layout; private Builder builder; private AlertDialog dialog; private MyAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LayoutInflater inflater = LayoutInflater.from(this); layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null); gridView = (GridView) layout.findViewById(R.id.grid); adapter = new MyAdapter(this); gridView.setAdapter(adapter); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); textView = (TextView) findViewById(R.id.textview); builder = new Builder(TestAndroid9Activity.this); builder.setView(layout); button1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub if(dialog == null){ dialog = builder.show(); } dialog.show(); } }); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub button1.setText(getResources().getStringArray(R.array.city)[position]); dialog.dismiss(); adapter.map.put(index, false); adapter.map.put(position, true); index = position; adapter.notifyDataSetChanged(); } }); } private class MyAdapter extends BaseAdapter{ private Context context; private String[] citys; private LayoutInflater inflater; public HashMapmap; public MyAdapter(Context context) { super(); this.context = context; citys = context.getResources().getStringArray(R.array.city); inflater = LayoutInflater.from(context); map = new HashMap (); for(int i=0;i
bank_grid.xml
<?xml version="1.0" encoding="utf-8"?>
bank_item.xml
<?xml version="1.0" encoding="utf-8"?>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。