Android之Spinner用法

本文详细介绍了一个Android应用中UI Spinner控件的具体实现方法。包括如何创建Spinner控件,设置适配器,监听用户的选择事件,并保存用户的选择状态。此外还介绍了如何在应用的不同生命周期回调方法中读取和写入Spinner的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 简单认识

Activity

package cn.lyj.android; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class AndroidUIActivity extends Activity { protected int mPos; protected String mSelection; protected ArrayAdapter<CharSequence> mAdapter; public static final int DEFAULT_POSITION = 2; public static final String PREFERENCES_FILE = "SpinnerPrefs"; public static final String PROPERTY_DELIMITER = "="; public static final String POSITION_KEY = "Position"; public static final String SELECTION_KEY = "Selection"; public static final String POSITION_MARKER = POSITION_KEY + PROPERTY_DELIMITER; public static final String SELECTION_MARKER = SELECTION_KEY + PROPERTY_DELIMITER; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.Spinner01); // 设置ArrayAdapter this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets, android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(this.mAdapter); // 选中触发事件,在窗口中央显示项目 OnItemSelectedListener spinnerListener = new myOnItemSelectedListener( this, this.mAdapter); spinner.setOnItemSelectedListener(spinnerListener); } /** * 用户选中下拉列表中得一项 */ public class myOnItemSelectedListener implements OnItemSelectedListener { ArrayAdapter<CharSequence> mLocalAdapter; Activity mLocalContext; // 构造方法 public myOnItemSelectedListener(Activity c, ArrayAdapter<CharSequence> ad) { this.mLocalContext = c; this.mLocalAdapter = ad; } //When the user selects an item in the spinner, this method is invoked by the callback chain public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) { //暂时没发现有什么用 //AndroidUIActivity.this.mPos = pos; AndroidUIActivity.this.mSelection = parent.getItemAtPosition(pos).toString(); TextView resultText = (TextView) findViewById(R.id.SpinnerResult); resultText.setText(AndroidUIActivity.this.mSelection); } //无用 public void onNothingSelected(AdapterView<?> parent) { } } /* * Restores the current state of the spinner (which item is selected, and the value * of that item). */ @Override public void onResume() { super.onResume(); if (!readInstanceState(this)) setInitialState(); Spinner restoreSpinner = (Spinner) findViewById(R.id.Spinner01); restoreSpinner.setSelection(getSpinnerPosition()); } /** * Read the previous state of the spinner from the preferences file */ public boolean readInstanceState(Context c) { SharedPreferences p = c.getSharedPreferences(PREFERENCES_FILE, MODE_WORLD_READABLE); this.mPos = p.getInt(POSITION_KEY, AndroidUIActivity.DEFAULT_POSITION); this.mSelection = p.getString(SELECTION_KEY, ""); return (p.contains(POSITION_KEY)); } /* *Store the current state of the spinner (which item is selected, and the value of that item). */ @Override public void onPause() { super.onPause(); if (!writeInstanceState(this)) { Toast.makeText(this, "Failed to write state!", Toast.LENGTH_LONG) .show(); } } /** * Write the application's current state to a properties repository. */ public boolean writeInstanceState(Context c) { SharedPreferences p = c.getSharedPreferences( AndroidUIActivity.PREFERENCES_FILE, MODE_WORLD_READABLE); SharedPreferences.Editor e = p.edit(); e.putInt(POSITION_KEY, this.mPos); e.putString(SELECTION_KEY, this.mSelection); return (e.commit()); } /** * Sets the initial state of the spinner when the application is first run. */ public void setInitialState() { this.mPos = DEFAULT_POSITION; } public int getSpinnerPosition() { return this.mPos; } public void setSpinnerPosition(int pos) { this.mPos = pos; } public String getSpinnerSelection() { return this.mSelection; } public void setSpinnerSelection(String selection) { this.mSelection = selection; } }

main.xml

<?xml version="1.0" encoding="utf-8"?> <!-- Creates a Linear Layout View to contain the spinner --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- Creates a spinner widget called Spinner01, within the Linear Layout The prompt text comes from the string "planet_prompt" in strings.xml Spinner不支持android:drawSelectorOnTop此属性 --> <Spinner android:id="@+id/Spinner01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:prompt="@string/planet_prompt" > </Spinner> <!-- Creates a TextView called SpinnerResult, below the spinner. --> <TextView android:id="@+id/SpinnerResult" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="Result" android:textSize="10pt" android:textStyle="bold" > </TextView> </LinearLayout>
strings.xml

<?xml version="1.0" encoding="utf-8"?> <!-- The string named "app_name" defines the application's visible name. The string array "Planets" defines an array of 9 strings. The application loads this array into the spinner's array adapter. The string "planet_prompt" defines the prompt for the result text box. --> <resources> <string name="app_name">Spinner</string> <string-array name="Planets"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> <item>Pluto</item> </string-array> <string name="planet_prompt">Select a planet</string> </resources>
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.lyj.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".AndroidUIActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
二. 运行结果

启动



点击菜单


选择Pluto


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值