7. Android advanced controls (1) (drop-down list)

The usage of the drop-down box and the basic concept of the adapter, combined with the instructions for the use of the drop-down box Spinner, respectively explain the specific usage and display effect of the array adapter ArrayAdapter and the simple adapter SimpleAdapter.

 1. Drop-down box Spinner

1. Spinner is used to select an item from a string of lists, and its function is similar to the combination of radio buttons.

2. The spinnerMode attribute in the XML file has two values:

dropdown: drop-down list form

dialog: dialog form

3. The following 4 methods can be called in Java code.

setPrompt: Set the title text.

setAdapter: Set the adapter for the drop-down list.

setSelection: Set which item is currently selected.

setOnItemSelectedListener: Sets the selection listener for the drop-down list

 2. Array Adapter

The drop-down box calls the setAdapter method to set the list adapter, and the simplest adapter is the array adapter.

Using the array adapter is divided into the following steps:

(1) Write the XML file of the list item, the internal layout has only one TextView tag

(2) Call the construction method of ArrayAdapter, fill in the string array to be displayed, and the XML file of the list item (R.layout.item_select)

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

context: context, just write this.

resource: sub-layout item

textViewResourceId: The id of the control that adapts the content of which textView in the layout.

objects: data source datas
 

(3) Call the setAdapter method of the drop-down box control and pass in the adapter instance obtained in the second step

package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerDropdownActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_dropdown);
        initSpinnerForDropdown(); // 初始化下拉模式的列表框
    }

    // 初始化下拉模式的列表框
    private void initSpinnerForDropdown() {
        // 声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, starArray);
        // 从布局文件中获取名叫sp_dropdown的下拉框
        Spinner sp_dropdown = findViewById(R.id.sp_dropdown);
        // 设置下拉框的标题。对话框模式才显示标题,下拉模式不显示标题
        sp_dropdown.setPrompt("请选择行星");
        sp_dropdown.setAdapter(starAdapter); // 设置下拉框的数组适配器
        sp_dropdown.setSelection(0); // 设置下拉框默认显示第一项
        // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
        sp_dropdown.setOnItemSelectedListener(new MySelectedListener());
    }

    // 定义下拉列表需要显示的文本数组
    private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
    // 定义一个选择监听器,它实现了接口OnItemSelectedListener
    class MySelectedListener implements OnItemSelectedListener {
        // 选择事件的处理方法,其中arg2代表选择项的序号
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(SpinnerDropdownActivity.this, "您选择的是" + starArray[arg2],
                    Toast.LENGTH_LONG).show();
        }

        // 未选择时的处理方法,通常无需关注
        public void onNothingSelected(AdapterView<?> arg0) {}
    }

}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:singleLine="true"
    android:gravity="center"
    android:textSize="17sp"
    android:textColor="#0000ff" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="下拉模式的列表框"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Spinner
        android:id="@+id/sp_dropdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>

</LinearLayout>

 

 

 8.1.3 Simple Adapter SimpleAdapter

ArrayAdapter can only display a text list, and SimpleAdapter allows displaying both text and pictures in list items.

To use a simple adapter, you need to specify a text array and an image array at the same time. The following is a code example for using SimpleAdapter: // Declare a simple adapter for a drop-down list, which specifies two sets of data for icons and text    

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); // Set the simple adapter for the drop-down box

package com.example.chapter05;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpinnerIconActivity extends AppCompatActivity {
    // 定义下拉列表需要显示的行星图标数组
    private Object[] iconArray = {R.drawable.icon_point_c, R.drawable.list_selector,
            R.drawable.icon_point_c, R.drawable.icon_point_c, R.drawable.icon_point_c,
            R.drawable.shape_oval_red};// 定义下拉列表需要显示的行星名称数组
    private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_icon);
        initSpinnerForSimpleAdapter(); // 初始化下拉框,演示简单适配器
    }

    private void initSpinnerForSimpleAdapter(){
        List<Map<String,Object>> list=new ArrayList<>();
        for(int i=0;i<iconArray.length;i++){
            Map<String,Object>item=new HashMap<>();
            item.put("icon",iconArray[i]);
            item.put("name",starArray[i]);
            list.add(item);
        }
        SimpleAdapter starAdapter = new SimpleAdapter(this, list,
                R.layout.item_simple, new String[]{"icon", "name"},
                new int[]{R.id.iv_icon, R.id.tv_name});
        starAdapter.setDropDownViewResource(R.layout.item_simple);
        // 从布局文件中获取名叫sp_icon的下拉框
        Spinner sp_icon = findViewById(R.id.sp_icon);
        sp_icon.setPrompt("请选择行星"); // 设置下拉框的标题
        sp_icon.setAdapter(starAdapter); // 设置下拉框的简单适配器
        sp_icon.setSelection(0); // 设置下拉框默认显示第一项
        // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
        sp_icon.setOnItemSelectedListener(new MySelectedListener());
    }
    // 定义一个选择监听器,它实现了接口OnItemSelectedListener
    class MySelectedListener implements AdapterView.OnItemSelectedListener {
        // 选择事件的处理方法,其中arg2代表选择项的序号
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(SpinnerIconActivity.this, "您选择的是" + starArray[arg2], Toast.LENGTH_LONG).show();
        }

        // 未选择时的处理方法,通常无需关注
        public void onNothingSelected(AdapterView<?> arg0) {}
    }

}


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- 这是展示行星图标的ImageView -->
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <!-- 这是展示行星名称的TextView -->
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:gravity="center"
        android:textColor="#ff0000"
        android:textSize="17sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="行星的简单适配器"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Spinner
        android:id="@+id/sp_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog" />"

</LinearLayout>

 

 

Guess you like

Origin blog.csdn.net/weixin_62190821/article/details/126863635