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

Android中Spanner获取选中内容和选中位置,根据位置选择对象的更多相关文章

  1. Android中调用文件管理器并返回选中文件的路径

    实际项目中经常需要调用文件管理器,选择下载路径或者上传的本地文件路径.今天就给大家做个demo示范该功能的实现过程. 一.实现效果预览 以下为三星S6的样机测试效果,当然不同手机调用后的效果不一样. ...

  2. Android中实现APP文本内容的分享发送与接收方法简述

    谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要 ...

  3. android中ContentProvider获取联系人 总结

    35.内容提供者:ContentResolver 用内容提供者来获取联系人信息 35-1:权限 <!-- 对联系人的读.写权限 --> <uses-permission androi ...

  4. Android 中如何获取 H5 保存在 LocalStorage 的数据

    主要分三步: 写个接口,接收 Js 回调 添加到 WebView 主动调用 Js 获取 比如我要获取保存在 LocalStorage 中的 userKey 字段: 1.写个接口,接收 Js 回调 pu ...

  5. android 中怎样获取IMEI号

    1)在Telephony Framework内部.能够直接使用GSMPhone或GeminiPhone提供的接口. KK之前的版本号: IMEI(International Mobile Equipm ...

  6. android中如何获取xml界面里的非自定义属性

    获取自定义属性大家都很熟悉了,就不多说了(定义declare-styleable,context.obtainStyledAttributes巴拉巴拉...) 下面我们说一下怎么获取非自定义的属性,比 ...

  7. Android中怎样获取SD卡路径

    很多时候我们需要将我们的数据或者apk保存到SD卡中,但是使用绝对路径可能会遇到错误,怎样解决这个问题呢?     可以通过以下方法获取SD卡的路径: Environment.getExternalS ...

  8. Android中onActivityResult()获取返回值

    需求:从FirstActivity跳到SecondActivity,在SecondActivity中进行了操作并返回到FirstActivity. FirstActivity中的主要代码: priva ...

  9. Android中StatFs获取系统/sdcard存储(剩余空间)大小

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

随机推荐

  1. margin的使用方法与技巧

    1.margin还可以用来做平移,作用类似translate哈哈.将元素设成absolute后就可以用margin随便平移他了,既不像relative那样要霸占空间,又不用为父元素设置relative ...

  2. Linq之Lambda表达式

    一 什么是LINQ? LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性. ...

  3. 运行QQ出现initialization failure 0x0000000c错误和浏览器上不了网

    出现QQ出现initialization failure 0x0000000c错误和浏览器上不了网的问题,原因是关机的时候没有正常关闭导致的. 解决方法: 1.我们在开始菜单栏中的附件中找到“命令提示 ...

  4. C# Web Forms - Using jQuery FullCalendar

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...

  5. EffectiveJava——接口优于抽象类

    Java程序设计语言提供两种机制,可以用来定义允许多个实现的类型:接口和抽象方法,这两者直接醉为明显的区别在于,抽象类允许某些方法的实现,但接口不允许,一个更为重要的区别在于,为了实现由抽象类定义的类 ...

  6. [WP8] 使用ApplicationMenu与使用者互动

    [WP8] 使用ApplicationMenu与使用者互动 范例下载 范例程序代码:点此下载 功能说明 使用过Lumia系列手机的开发人员,对于内建的相机功能相信都很熟悉.在Lumia内建的相机功能中 ...

  7. js填写银行卡号,每隔4位数字加一个空格

    1.原生js写法 !function () { document.getElementById('bankCard').onkeyup = function (event) { var v = thi ...

  8. VisualStudio中解决方案

    在VS中创建一个项目通常会生成一个解决方案文件(.sln)和一个隐藏的解决方案用户选项文件(.suo). 解决方案文件是一个文本文件,包含以下信息: 将被加载的所有项目以构成完整解决方案的项目清单 解 ...

  9. MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )

    The maximum number of controls allowed on MSCRM dashboards are 6. You cannot put the more than 6 gra ...

  10. Iterator 迭代器(一)

         迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为 ...