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

作为一名菜鸟,关于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 = 
2 ;
                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
        }
    }
}

 

转载于:https://www.cnblogs.com/hopecapital/p/4609372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值