代码实现:
下拉列表内容可以在后台获取存放到数组中,可以在前台strings.xml中定义
strings.xml
<?xml version="1.0" encoding="utf-8"?>SpinnerDemo
Hello world!
选择性别
男
女
activity_main.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/LinearLayout1”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=“ r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}” >
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“性别” />
<Spinner
android:id=“@+id/spinner1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:prompt=“@string/title”
android:entries=“@array/sex”/>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“课程” />
<Spinner
android:id=“@+id/spinner2”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<Button
android:id=“@+id/button”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“显示选项” />
MainActivity.java
package com.example.spinnerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
最后
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长。而不成体系的学习效果低效漫长且无助。时间久了,付出巨大的时间成本和努力,没有看到应有的效果,会气馁是再正常不过的。
所以学习一定要找到最适合自己的方式,有一个思路方法,不然不止浪费时间,更可能把未来发展都一起耽误了。
如果你是卡在缺少学习资源的瓶颈上,那么刚刚好我能帮到你。