Android中Spinner控件之鍵值對用法實例分析
更新時間:2015年09月21日 14:52:11 作者:Ruthless
這篇文章主要介紹了Android中Spinner控件之鍵值對用法,實例分析了Spinner控件控件的鍵值對實用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android中Spinner控件之鍵值對用法。分享給大家供大家參考。具體如下:
一、字典表,用來存放鍵值對信息
package com.ljq.activity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Dict implements Serializable {
private Integer id;
private String text;
public Dict() {
}
public Dict(Integer id, String text) {
super();
this.id = id;
this.text = text;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
/**
* 為什么要重寫toString()呢?
*
* 因為適配器在顯示數(shù)據(jù)的時候,如果傳入適配器的對象不是字符串的情況下,直接就使用對象.toString()
*/
@Override
public String toString() {
return text;
}
}
二、activity類,綁定數(shù)據(jù)、獲取選中的鍵值對
package com.ljq.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
private Spinner mySpinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySpinner = (Spinner) findViewById(R.id.mySpinner);
List<Dict> dicts = new ArrayList<Dict>();
dicts.add(new Dict(1, "測試1"));
dicts.add(new Dict(2, "測試2"));
dicts.add(new Dict(3, "測試3"));
dicts.add(new Dict(4, "測試4"));
ArrayAdapter<Dict> adapter = new ArrayAdapter<Dict>(this,
android.R.layout.simple_spinner_item, dicts);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 獲取鍵的方法:mySpinner.getSelectedItem().toString()或((Dict)mySpinner.getSelectedItem()).getId()
// 獲取值的方法:((Dict)mySpinner.getSelectedItem()).getText();
Toast.makeText(MainActivity.this,
"鍵:" + mySpinner.getSelectedItem().toString() + "、" + ((Dict) mySpinner.getSelectedItem()).getId() +
",值:" + ((Dict) mySpinner.getSelectedItem()).getText(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
三、修改main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mySpinner"/> </LinearLayout>
四、運行結(jié)果如下:

希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android?SharedPreferences性能瓶頸解析
這篇文章主要為大家介紹了Android?SharedPreferences性能瓶頸解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android編程之線性布局LinearLayout實例簡析
這篇文章主要介紹了Android編程之線性布局LinearLayout用法,結(jié)合實例形式簡單分析了Android線性布局的使用技巧,需要的朋友可以參考下2016-01-01
解決Android studio用真機調(diào)試時logcat一直輸出日志問題
這篇文章主要介紹了解決Android studio用真機調(diào)試時logcat一直輸出日志問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Ubuntu中為Android系統(tǒng)實現(xiàn)內(nèi)置Java應(yīng)用程序測試Application Frameworks層的硬件服務(wù)
本文主要介紹Ubuntu中為Android系統(tǒng)內(nèi)置應(yīng)用訪問Application Frameworks層的硬件服務(wù),這里提供了詳細(xì)的流程和代碼實例,有興趣的朋友可以參考下2016-08-08

