下拉框的使用——详解

学习下拉框,首先必须要了解适配器

适配器(Adapter)

作用:适配器负责从数据集合中取出对应的数据显示到条目布局上

所以在需要用到适配器时,需要创建一个条目布局

例如:在layout布局下创建一个TextView的条目

Adapter的基本子类有ArrayAdapter(数组适配器)和SimpleAdapter(简单适配器)

数组适配器就是中是一些文字,而简单适配器中可以包含图片和文字

数组适配器

数组适配器的使用步骤:

        1.xml文件布局只有一个TextView

        2.调用ArrayAdapter的构造方法,填入展现的数组,以及xml文件

        3.调用下拉框控件的setAdapter方法,传入第二步得到的适配器实例

直接上代码

Java代码

  1. package com.example.chapter6;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Adapter;
  6. import android.widget.AdapterView;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Spinner;
  9. import android.widget.Toast;
  10. public class adapterViewActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  11.     private Spinner sp_dropdown;
  12.     private final static String[] array = new String[]{"火星","月球","地球"};
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_adapter_view);
  17.         sp_dropdown = findViewById(R.id.sp_dropdown);//找到下拉框的id
  18.         ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this,R.layout.item_selector,array);//创建适配器
  19.         //适配器的参数,首先是上下文,其次,需要在布局文件下创建一个名字为item_selector的TextView的条目布局,最后一个参数是数组,就是将这个数组中的数据传给适配器,再由适配器放到下拉框上
  20.         sp_dropdown.setAdapter(stringArrayAdapter);//下拉框配置适配器
  21.         sp_dropdown.setSelection(0);//默认选中第一个元素
  22.         sp_dropdown.setOnItemSelectedListener(this);//事件监听
  23.     }
  24.     @Override
  25.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  26.         Toast.makeText(adapterViewActivity.this,"您的选择是"+array[position],Toast.LENGTH_SHORT).show();
  27.     }
  28.     @Override
  29.     public void onNothingSelected(AdapterView<?> parent) {
  30.     }
  31. }

xml条目文件

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="50dp"
  5.     android:gravity="center"
  6.     android:textColor="#000000"
  7.     android:textSize="25sp"/>

xml属性文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="horizontal">
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="50dp"
  11.         android:text="选择适合的星球"
  12.         android:textSize="25sp"
  13.         android:textColor="#000000"/>
  14.     <Spinner
  15.         android:id="@+id/sp_dropdown"
  16.         android:layout_width="0dp"
  17.         android:layout_weight="1"
  18.         android:layout_height="50dp"
  19.         android:spinnerMode="dropdown"/>
  20. </LinearLayout>

简单适配器SimpleAdapter

简单适配器是同时拥有文字和图片,所以条目布局中设置一个ImageView 和一个TextView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content">
  6.     <!--普通布局的条目-->
  7.     <!--一个image,一个text-->
  8.     <ImageView
  9.         android:id="@+id/iv_scope"
  10.         android:layout_width="0dp"
  11.         android:layout_weight="1"
  12.         android:layout_height="50dp"
  13.         />
  14.     <TextView
  15.         android:layout_width="0dp"
  16.         android:layout_weight="3"
  17.         android:layout_height="match_parent"
  18.         android:textSize="25sp"
  19.         android:textColor="#000000"
  20.         android:gravity="center" />
  21. </LinearLayout>

java代码

  1. package com.example.chapter6;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.LinearLayout;
  7. import android.widget.SimpleAdapter;
  8. import android.widget.Spinner;
  9. import android.widget.Toast;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. public class SimpleAdapterActivity extends AppCompatActivity {
  15.     private static final int[] iconArray = {
  16.             R.drawable.oppo,R.drawable.iphone,R.drawable.ronyao,
  17.             R.drawable.vivo,R.drawable.xiaomi,R.drawable.mate
  18.     };
  19.     private static final String[] starArray = {"oppo","苹果","荣耀","vivo","小米","mate"};
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_simple_adapter);
  24.         LinearLayout ite_simple = findViewById(R.layout.item_simple);
  25.         List<Map<String,Object>> list = new ArrayList<>();
  26.         //循环存储每个元素,创建一个map,每个list中都有两个,key为icon,value 为name 的map
  27.         //依次对应
  28.         for (int i = 0; i < iconArray.length; i++) {
  29.             Map<String,Object> map = new HashMap<>();
  30.             map.put("icon",iconArray[i]);
  31.             map.put("name",starArray[i]);
  32.             list.add(map);
  33.         }
  34.         //声明一个简单适配器的,指定固定图形与文本两组数据
  35.         SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.item_simple,
  36.                 new String[]{"icon","name"},new int[]{R.id.iv_icon,R.id.tv_name});
  37.         //第一个参数:上下文,第二个参数:条目布局,第三个是每个list中的map的key,第四个就是对应list中map的key对应的数据的组件
  38.         Spinner sp_simple = findViewById(R.id.sp_simple);
  39.         sp_simple.setAdapter(simpleAdapter);//添加适配器
  40.         sp_simple.setSelection(0);//设置初始值,就是一开始默认选择的值
  41.         sp_simple.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  42.             @Override
  43.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  44.                 Toast.makeText(SimpleAdapterActivity.this, "选择好了", Toast.LENGTH_SHORT).show();
  45.             }
  46.             @Override
  47.             public void onNothingSelected(AdapterView<?> parent) {
  48.             }
  49.         });
  50.     }
  51. }

配置xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content">
  6.     <!--普通布局的条目-->
  7.     <!--一个image,一个text-->
  8.     <ImageView
  9.         android:id="@+id/iv_icon"
  10.         android:layout_width="0dp"
  11.         android:layout_weight="1"
  12.         android:layout_height="50dp"
  13.         />
  14.     <TextView
  15.         android:id="@+id/tv_name"
  16.         android:layout_width="0dp"
  17.         android:layout_weight="3"
  18.         android:layout_height="match_parent"
  19.         android:textSize="25sp"
  20.         android:textColor="#000000"
  21.         android:gravity="center" />
  22. </LinearLayout>
  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值