当前位置:   article > 正文

7、android 高级控件(1)(下拉列表)_android下拉框控件

android下拉框控件

下拉框的用法以及适配器的基本概念,结合对下拉框Spinner的使用说明分别阐述数组适配器ArrayAdapter、简单适配器SimpleAdapter的具体用法与展示效果。

 1、下拉框Spinner

1、Spinner用于从一串列表中选择某项,功能类似于单选按钮的组合。

2、XML文件中的spinnerMode属性有两种取值:

dropdown:下拉列表形式

dialog:对话框形式

3、在Java代码中可调用下列4个方法。

setPrompt:设置标题文字。

setAdapter:设置下拉列表的适配器。

setSelection:设置当前选中哪项。

setOnItemSelectedListener:设置下拉列表的选择监听器

 2、数组适配器

下拉框调用setAdapter方法设置列表适配器,最简单的适配器就是数组适配器。

运用数组适配器分成下列步骤:

(1)编写列表项的XML文件,内部布局只有一个TextView标签

(2)调用ArrayAdapter的构造方法,填入待展现的字符串数组,以及列表项的XML文件(R.layout.item_select)

new ArrayAdapter(context, resource, textViewResourceId, objects);

context:上下文,写this就好了。

resource: 子布局item

textViewResourceId:对布局中哪个textView进行内容适配的控件的id。

objects: 数据源datas
 

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

  1. package com.example.chapter05;
  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.AdapterView.OnItemSelectedListener;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Spinner;
  9. import android.widget.Toast;
  10. public class SpinnerDropdownActivity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_spinner_dropdown);
  15. initSpinnerForDropdown(); // 初始化下拉模式的列表框
  16. }
  17. // 初始化下拉模式的列表框
  18. private void initSpinnerForDropdown() {
  19. // 声明一个下拉列表的数组适配器
  20. ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,
  21. R.layout.item_select, starArray);
  22. // 从布局文件中获取名叫sp_dropdown的下拉框
  23. Spinner sp_dropdown = findViewById(R.id.sp_dropdown);
  24. // 设置下拉框的标题。对话框模式才显示标题,下拉模式不显示标题
  25. sp_dropdown.setPrompt("请选择行星");
  26. sp_dropdown.setAdapter(starAdapter); // 设置下拉框的数组适配器
  27. sp_dropdown.setSelection(0); // 设置下拉框默认显示第一项
  28. // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
  29. sp_dropdown.setOnItemSelectedListener(new MySelectedListener());
  30. }
  31. // 定义下拉列表需要显示的文本数组
  32. private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
  33. // 定义一个选择监听器,它实现了接口OnItemSelectedListener
  34. class MySelectedListener implements OnItemSelectedListener {
  35. // 选择事件的处理方法,其中arg2代表选择项的序号
  36. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  37. Toast.makeText(SpinnerDropdownActivity.this, "您选择的是" + starArray[arg2],
  38. Toast.LENGTH_LONG).show();
  39. }
  40. // 未选择时的处理方法,通常无需关注
  41. public void onNothingSelected(AdapterView<?> arg0) {}
  42. }
  43. }
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/tv_name"
  3. android:layout_width="match_parent"
  4. android:layout_height="50dp"
  5. android:singleLine="true"
  6. android:gravity="center"
  7. android:textSize="17sp"
  8. android:textColor="#0000ff" />
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="5dp">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:text="下拉模式的列表框"
  12. android:textColor="@color/black"
  13. android:textSize="17sp"/>
  14. <Spinner
  15. android:id="@+id/sp_dropdown"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:spinnerMode="dropdown"/>
  19. </LinearLayout>

 

 

 8.1.3  简单适配器SimpleAdapter

ArrayAdapter只能显示文本列表,SimpleAdapter允许在列表项中同时展示文本与图片。

使用简单适配器需要同时指定文本数组与图片数组,下面是SimpleAdapter的使用代码例子:     // 声明一个下拉列表的简单适配器,其中指定了图标与文本两组数据    

SimpleAdapter starAdapter = new SimpleAdapter(this, list,            

R.layout.item_simple, new String[]{"icon", "name"},            

new int[]{R.id.iv_icon, R.id.tv_name});    

sp.setAdapter(starAdapter); // 设置下拉框的简单适配器

  1. package com.example.chapter05;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.AdapterView;
  5. import android.widget.SimpleAdapter;
  6. import android.widget.Spinner;
  7. import android.widget.Toast;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. public class SpinnerIconActivity extends AppCompatActivity {
  15. // 定义下拉列表需要显示的行星图标数组
  16. private Object[] iconArray = {R.drawable.icon_point_c, R.drawable.list_selector,
  17. R.drawable.icon_point_c, R.drawable.icon_point_c, R.drawable.icon_point_c,
  18. R.drawable.shape_oval_red};// 定义下拉列表需要显示的行星名称数组
  19. private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
  20. @Override
  21. protected void onCreate(@Nullable Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_spinner_icon);
  24. initSpinnerForSimpleAdapter(); // 初始化下拉框,演示简单适配器
  25. }
  26. private void initSpinnerForSimpleAdapter(){
  27. List<Map<String,Object>> list=new ArrayList<>();
  28. for(int i=0;i<iconArray.length;i++){
  29. Map<String,Object>item=new HashMap<>();
  30. item.put("icon",iconArray[i]);
  31. item.put("name",starArray[i]);
  32. list.add(item);
  33. }
  34. SimpleAdapter starAdapter = new SimpleAdapter(this, list,
  35. R.layout.item_simple, new String[]{"icon", "name"},
  36. new int[]{R.id.iv_icon, R.id.tv_name});
  37. starAdapter.setDropDownViewResource(R.layout.item_simple);
  38. // 从布局文件中获取名叫sp_icon的下拉框
  39. Spinner sp_icon = findViewById(R.id.sp_icon);
  40. sp_icon.setPrompt("请选择行星"); // 设置下拉框的标题
  41. sp_icon.setAdapter(starAdapter); // 设置下拉框的简单适配器
  42. sp_icon.setSelection(0); // 设置下拉框默认显示第一项
  43. // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
  44. sp_icon.setOnItemSelectedListener(new MySelectedListener());
  45. }
  46. // 定义一个选择监听器,它实现了接口OnItemSelectedListener
  47. class MySelectedListener implements AdapterView.OnItemSelectedListener {
  48. // 选择事件的处理方法,其中arg2代表选择项的序号
  49. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  50. Toast.makeText(SpinnerIconActivity.this, "您选择的是" + starArray[arg2], Toast.LENGTH_LONG).show();
  51. }
  52. // 未选择时的处理方法,通常无需关注
  53. public void onNothingSelected(AdapterView<?> arg0) {}
  54. }
  55. }

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <!-- 这是展示行星图标的ImageView -->
  6. <ImageView
  7. android:id="@+id/iv_icon"
  8. android:layout_width="0dp"
  9. android:layout_height="50dp"
  10. android:layout_weight="1" />
  11. <!-- 这是展示行星名称的TextView -->
  12. <TextView
  13. android:id="@+id/tv_name"
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_weight="3"
  17. android:gravity="center"
  18. android:textColor="#ff0000"
  19. android:textSize="17sp" />
  20. </LinearLayout>
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="5dp" >
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center"
  10. android:text="行星的简单适配器"
  11. android:textColor="@color/black"
  12. android:textSize="17sp" />
  13. <Spinner
  14. android:id="@+id/sp_icon"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:spinnerMode="dialog" />"
  18. </LinearLayout>

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/111532
推荐阅读
相关标签
  

闽ICP备14008679号