Android中自定义xml文件给Spinner下拉框赋值并获取下拉选中的值

摘要:
˃全部公众号霸道的程序猿博客霸道流氓气质只要通过name属性赋值为ctype,后续被引用。然后再回到activity_spinner.xml中,通过android:entries="@array/ctype"为下拉框设置选项数组内容。˃然后来到activity,通过id获取spinner,然后设置其选项被选中的事件监听器,获取选中值的内容并输出packagecom.badao.relativelayouttest;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.Spinner;importandroid.widget.Toast;publicclassSpinnerActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate{super.onCreate;setContentView;Spinnerspinner=findViewById;spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){@OverridepublicvoidonItemSelected(AdapterView˂?
场景

实现效果如下

Android中自定义xml文件给Spinner下拉框赋值并获取下拉选中的值第1张

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加id属性。

然后在res下values下新建arrays.xml,数组资源文件,用来存储下拉框的选项内容

Android中自定义xml文件给Spinner下拉框赋值并获取下拉选中的值第2张

arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="ctype">
        <item>全部</item>
        <item>公众号</item>
        <item>霸道</item>
        <item>的</item>
        <item>程序猿</item>
        <item>博客</item>
        <item>霸道</item>
        <item>流氓</item>
        <item>气质</item>
    </string-array>
</resources>

只要通过name属性赋值为ctype,后续被引用。

然后再回到activity_spinner.xml中,通过

android:entries="@array/ctype"

为下拉框设置选项数组内容。

activity_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".SpinnerActivity">

    <Spinner
        android:id="@+id/spinnner"android:entries="@array/ctype"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

</LinearLayout>

然后来到activity,通过id获取spinner,然后设置其选项被选中的事件监听器,获取选中值的内容并输出

package com.badao.relativelayouttest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public classSpinnerActivity extends AppCompatActivity {

    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        Spinner spinner =(Spinner) findViewById(R.id.spinnner);
        spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, longid) {
                String result =parent.getItemAtPosition(position).toString();
                Toast.makeText(SpinnerActivity.this,result,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?>parent) {

            }
        });
    }
}

免责声明:文章转载自《Android中自定义xml文件给Spinner下拉框赋值并获取下拉选中的值》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇oracle查询中时间格式由日月年转换为年月日pycharm设置主题/默认格式/字体下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SQL Server中将多行数据拼接为一行数据(一个字符串)

SQLServer中将多行数据拼接为一行数据(一个字符串) 方法一: 使用T-SQL DECLARE @Users NVARCHAR(MAX) SET @Users = '' SELECT @Users = @Users + ',' + UserName FROM dbo.[User] WHERE RoleID = 1 SELECT @Users 转载...

Android系统如何管理自己内存的?

                            本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!         机缘巧合写下这篇博客,作为个人工作经验的总结,不足之处,随后补上。           安卓是基于Linux2.6内核的操开源作系统,安卓比Linux更智能在于:进程结束后,安卓会...

Oh my God, Swagger API文档竟然可以这样写?

最好的总会在不经意间出现。 作为后端程序员,免不了与前端同事对接API, 一个书写良好的API设计文档可有效提高与前端对接的效率。 为避免联调时来回撕逼,今天我们聊一聊正确使用Swaager的姿势。 基础Swagger用法 在ConfigureServices配置Swagger文档,在Configure启用中间件 // Install-Package S...

分析各种Android设备屏幕分辨率与适配

一. 数据采集    源码GitHub地址 :  -- SSH : git@github.com:han1202012/DisplayTest.git; -- HTTP : https://github.com/han1202012/DisplayTest;   . 使用下面的程序运行在不同设备上 :   package shuliang.han.dis...

[置顶] xamarin android使用gps定位获取经纬度

看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位、基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用的,肯定是无法获取获取位置信息(当然也肯定是有办法解决这种问题的) 3.android 的GPS定位需要一定时间的,大概一分钟左右。 4.andro...

学Android开发 这19个开发工具助你顺风顺水

要想快速开发一个Android应用,通常会用到很多工具,巧妙利用这些工具,能让我们的开发工作事半功倍,节省大量时间,下面大连Android开发培训小编就为大家介绍下这19个开发工具都有神马用途。   1、XAppDbg   XAppDbg是一个可以在运行中改变代码中参数的一个应用开发工具。这个工具可以为你省下大量的时间,因为你不用为应用的每次小改变而重新...