zoukankan      html  css  js  c++  java
  • 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
            }
        }
    }
    ————————————————————————————————————————————————
    来自企鹅娘的问候:
    欢迎交流哦,如果有帮助转载的话,请务必注明出处"企鹅娘's 学习笔记",让我也小小的开心一下
  • 相关阅读:
    Python OpenCV 常用操作
    Conda Cheatsheet | 速查表
    Loadrunner解决启动浏览器后页面显示空白
    26个ASP.NET常用性能优化方法
    C# Foreach用法
    体验ASP.NET MVC3 表单令牌功能!
    基于.Net(C#开发)平台的三层框架架构软件的设计与实现
    去掉浏览器中a标签的虚线
    Microsoft Dynamics CRM 4.0 序列号
    编写 Cookie
  • 原文地址:https://www.cnblogs.com/hopecapital/p/4609372.html
Copyright © 2011-2022 走看看