欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现多列显示的下拉列表框Spinner功能示例

程序员文章站 2023-11-13 16:42:16
本文实例讲述了android编程实现多列显示的下拉列表框spinner功能。分享给大家供大家参考,具体如下: 先上张效果图: 严格来说,这并不是一个下拉列表,只不过...

本文实例讲述了android编程实现多列显示的下拉列表框spinner功能。分享给大家供大家参考,具体如下:

先上张效果图:

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. */
@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 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程序设计有所帮助。