Explanation and actual combat of Spinner, an advanced control drop-down list in Android Studio App development (with source code, super detailed must-see)

If there is a problem with the operation or you need the source code, please like and follow the collection and leave a message in the comment area~~~

1. The drop-down box Spinner

Spinner is a drop-down box control. It is used to select an item from a list. Its function is similar to the combination of radio buttons. There are two ways to display the drop-down list. One is to pop up a list box directly below the current drop-down box , and the other is to pop up a list dialog box in the middle of the page. In addition, Spinner can also call the following four methods in Java code

setPrompt set title text

setAdapter sets the data adapter of the list item

setSelection Set which item is currently selected

setOnItemSelectedListener Sets the selection listener for the drop-down list

The effect is as follows After clicking on it, the selection function can be realized and after clicking on an item, the dialog box disappears and the text in the drop-down box becomes the name of the planet just selected

 

 

 java class code

package com.example.chapter08;

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) {}
    }

}

XML file

<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>

Second, the array adapter ArrayAdapter

ArrayAdapter is mainly used in the case where each line list only displays text

Three, simple adapter SimpleAdapter

ArrayAdapter can only display a text list, which is obviously not beautiful enough, and sometimes I want to add icons to the list. The implementation process of SimpleAdapter is slightly complicated, it can display text and graphics at the same time. Here is the effect of using a simple adapter

It can be seen that there are texts and corresponding icons to display more beautifully

 

 

 Java class code

package com.example.chapter08;

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

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.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerIconActivity extends AppCompatActivity {

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

    // 初始化下拉框,演示简单适配器
    private void initSpinnerForSimpleAdapter() {
        // 声明一个映射对象的列表,用于保存行星的图标与名称配对信息
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // iconArray是行星的图标数组,starArray是行星的名称数组
        for (int i = 0; i < iconArray.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            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());
    }

    // 定义下拉列表需要显示的行星图标数组
    private int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
            R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
    // 定义下拉列表需要显示的行星名称数组
    private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};

    // 定义一个选择监听器,它实现了接口OnItemSelectedListener
    class MySelectedListener implements 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) {}
    }

}

XML file code

<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>

It's not easy to create and find it helpful, please like, follow and collect~~~

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/127757972