本文实例讲述了AndroID编程实现多列显示的下拉列表框Spinner功能。分享给大家供大家参考,具体如下:
先上张效果图:
严格来说,这并不是一个下拉列表,只不过实现的效果很像是下拉列表
实现原理:
该下拉列表其实是一个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. */@OverrIDepublic 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 HashMap<Integer,Boolean> map; public MyAdapter(Context context) { super(); this.context = context; citys = context.getResources().getStringArray(R.array.city); inflater = LayoutInflater.from(context); map = new HashMap<Integer,Boolean>(); for(int i=0;i<citys.length;i++){ map.put(i,false); } } public int getCount() { // Todo auto-generated method stub return citys.length; } public Object getItem(int position) { // Todo auto-generated method stub return null; } public long getItemID(int position) { // Todo auto-generated method stub return position; } public VIEw getVIEw(int position,VIEw vIEw,VIEwGroup parent) { // Todo auto-generated method stub if(vIEw == null){ vIEw = inflater.inflate(R.layout.bank_item,null); } TextVIEw textVIEw = (TextVIEw) vIEw.findVIEwByID(R.ID.List_text); Radiobutton radiobutton = (Radiobutton) vIEw.findVIEwByID(R.ID.radiobutton); radiobutton.setChecked(map.get(position)); textVIEw.setText(citys[position]); return vIEw; }}
bank_grID.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" androID:padding="20dp" androID:background="#ffffff" androID:ID="@+ID/grID_layout"> <GrIDVIEw androID:ID="@+ID/grID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:verticalSpacing="20px" androID:horizontalSpacing="10px" androID:numColumns="4" androID:scrollbars="vertical" androID:layout_margin="10dp"/></linearLayout>
bank_item.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="#ffffff" > <Radiobutton androID:ID="@+ID/radiobutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginRight="15dp" androID:focusable="false" androID:clickable="false" androID:focusableIntouchMode="false" /> <TextVIEw androID:ID="@+ID/List_text" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textcolor="#000000" androID:layout_gravity="center_vertical"/></linearLayout>
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现多列显示的下拉列表框Spinner功能示例全部内容,希望文章能够帮你解决Android编程实现多列显示的下拉列表框Spinner功能示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)