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

 更新时间:2017年06月27日 10:50:39   作者:Central-Perk  
这篇文章主要介绍了Android编程实现多列显示的下拉列表框Spinner功能,结合具体实例形式分析了Android多列表显示功能的相关布局操作实现技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • 自定义View系列之kotlin绘制手势设置温度控件的方法

    自定义View系列之kotlin绘制手势设置温度控件的方法

    这篇文章主要给大家介绍了关于自定义View系列之kotlin绘制手势设置温度控件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • Kotlin基本数据类型详解

    Kotlin基本数据类型详解

    大家好,本篇文章主要讲的是Kotlin基本数据类型详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Android开发Dart Constructors构造函数使用技巧整理

    Android开发Dart Constructors构造函数使用技巧整理

    这篇文章主要为大家介绍了Android开发Dart Constructors构造函数使用技巧整理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Android 使用Toolbar实现应用栏实例详解

    Android 使用Toolbar实现应用栏实例详解

    这篇文章主要为大家介绍了Android 使用Toolbar实现应用栏实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android LeakCanary检测内存泄露原理

    Android LeakCanary检测内存泄露原理

    这篇文章主要介绍了分析LeakCanary检测内存泄露原理,帮助大家更好的理解和学习使用Android开发,感兴趣的朋友可以了解下
    2021-03-03
  • Android-实现切换Fragment页功能的实现代码

    Android-实现切换Fragment页功能的实现代码

    本篇文章主要介绍了Android-实现切换Fragment页功能的实现代码,具有一定的参加价值,有兴趣的可以了解一下。
    2017-02-02
  • Android11绕过反射限制的方法

    Android11绕过反射限制的方法

    这篇文章主要介绍了Android11绕过反射限制的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android如何实现时间线效果

    Android如何实现时间线效果

    这篇文章主要介绍了 Android如何实现时间线效果,下面文章围绕 Android如何实现时间线效果的相关资料展开详细内容,具有一定的参考价值 ,需要的朋友可以参考一下
    2021-11-11
  • 详解如何使用VisualStudio高效开发调试AndroidNDK

    详解如何使用VisualStudio高效开发调试AndroidNDK

    这篇文章主要介绍了详解如何使用VisualStudio高效开发调试AndroidNDK,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Android gradle打包并自动上传的方法

    Android gradle打包并自动上传的方法

    本篇文章主要介绍了Android gradle打包并自动上传的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论