Android中Spanner获取选中内容和选中位置,根据位置选择对象

时间:2023-12-13 17:16:38

作为一名菜鸟,关于spanner获取选中的内容文字代码,网上后很多

但是根据给出的位置来自动选择对象,这个代码一直没找到

后来找人问了问,才知道就一句话的事,特意在这里记录下

array.xml

 XML Code 
1
2
3
4
5
6
7
8
9
10
11
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="elementsArray">
        <item>金</item>
        <item>木</item>
        <item>水</item>
        <item>火</item>
        <item>土</item>
    </string-array>

</resources>

activity_main.xml

 XML Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.spannerposition.MainActivity" >
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView 
            android:id="@+id/tv_elements"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择你喜欢的五行:"
            android:textSize="17sp"/>

<Spinner
        android:id="@+id/s_elements"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:entries="@array/elementsArray"
        android:textColor="#000000"
        android:textCursorDrawable="@null"
        android:textSize="17sp" />
    
    <Button 
        android:id="@+id/bn_s_select3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="选中第三个"
        android:textSize="17sp"/>
    </LinearLayout>

</RelativeLayout>

MainActivity.java

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
package com.example.spannerposition;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Spinner s_elements;
    private int s_position;//记录选择的位置
    private String element;
    private Button bn_select3;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.s_elements = (Spinner) super.findViewById(R.id.s_elements);
        this.s_elements.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
        this.bn_select3 = (Button) super.findViewById(R.id.bn_s_select3);
        
      //button监听
        bn_select3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                s_position = ;
                s_elements.setSelection(s_position, true);//设置为选中s_posiiton位置的元素
            }
            
        });
    }
 // 下拉框选择事件
    private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            element = parent.getItemAtPosition(position).toString();// 得到spanner的值
            s_position = position;
            Toast.makeText(MainActivity.this, "选择的元素是:" +
            element,Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
        }
    }
}