Android Spinner 組件的應(yīng)用實(shí)例
Android Spinner 組件
Spinner: 下拉組件
使用事項(xiàng):布局在XML 中實(shí)現(xiàn),具體的數(shù)據(jù)在JAVA 代碼中實(shí)現(xiàn);
所用知識點(diǎn):
數(shù)組適配器:ArrayAdapter 用于關(guān)系M 層和 C 層;
事件:OnItemSelectedListener;
案列:查看十二星座效果圖:
xml:代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/showInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text=" " />
</LinearLayout>
</ScrollView>
</LinearLayout>
java代碼如下:
package com.example.spinnertest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Resources;
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;
public class MainActivity extends Activity {
private Spinner spinner;
private TextView tx;
private ArrayList<String> list = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 獲取TextView 實(shí)體對象 , 用于顯示星座的詳細(xì)信息 */
tx = (TextView) findViewById(R.id.showInfo);
/* 下拉組件測試 Spinner 對象首先獲取 */
spinner = (Spinner) findViewById(R.id.spinner);
/* 準(zhǔn)備數(shù)據(jù)源 M , 用集合進(jìn)行保存 */
list = new ArrayList<String>();
list.add("Aries");
list.add("Taurus");
list.add("Gemini");
list.add("Cancer");
list.add("Leo");
list.add("Virgo");
list.add("Libra");
list.add("Scorpio");
list.add("Sagittarius");
list.add("Capricorn");
list.add("Aquarius");
list.add("Pisces");
/* 實(shí)現(xiàn)M 層 與C 層的關(guān)系 ,綁定數(shù)據(jù) */ /* 參數(shù)1:上下文對象; 參數(shù)2:系統(tǒng)資源布局方式 ; 參數(shù)3:數(shù)據(jù)對象 */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
/* 對V 層和C 層進(jìn)行關(guān)系的綁定; */
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
/* 對所有的選擇項(xiàng)進(jìn)行監(jiān)控 *//* 參3:當(dāng)前選擇項(xiàng)的ID 參4:被選擇項(xiàng)在組中的位置,邏輯上與ID 相等,但代表的意義不一樣 */
public void onItemSelected(AdapterView<?> arg0, View arg1,
int id, long position) {
// Toast.makeText(MainActivity.this, "你選擇的是第:"+id+"值為:"+list.get(id), 1000).show();
/* 設(shè)置tx對象的值 */
String temp = getConstellation(id);
tx.setText("\t"+temp);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
/* 顯示星座的信息 */
protected String getConstellation(int id){
Resources rs = getResources();
String temp = "";
switch(id){
case 0:
temp = rs.getString(R.string.Aries);
break;
case 1:
temp = rs.getString(R.string.Taurus);
break;
case 2:
temp = rs.getString(R.string.Gemini);
break;
case 3:
temp = rs.getString(R.string.Cancer);
break;
case 4:
temp = rs.getString(R.string.Leo);
break;
case 5:
temp = rs.getString(R.string.Virgo);
break;
case 6:
temp = rs.getString(R.string.Libra);
break;
case 7:
temp = rs.getString(R.string.Scorpio);
break;
case 8:
temp = rs.getString(R.string.Sagittarius);
break;
case 9:
temp = rs.getString(R.string.Capricorn);
break;
case 10:
temp = rs.getString(R.string.Aquarius);
break;
case 11:
temp = rs.getString(R.string.Pisces);
break;
case 12:
temp = rs.getString(R.string.Aries);
break;
}
return temp;
}
}
以上就是Android Spinner 組件的實(shí)例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實(shí)現(xiàn))
這篇文章主要介紹了Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號碼的方法,通過Android的PhoneStateListene實(shí)現(xiàn)該功能,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)在map上畫出路線的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在map上畫出路線的方法,較為詳細(xì)的分析了Android在map上繪制路線所涉及的map圖調(diào)用、畫筆的使用、頁面布局及權(quán)限控制的相關(guān)技巧,需要的朋友可以參考下2015-07-07
Android適配器Adapter與ListView和RecycleView的簡單使用
本文將為大家介紹Android開發(fā)中常用的適配器(Adapter)概念,以及ListView和RecycleView的簡單使用方法,需要的朋友可以參考下2023-05-05
Android ScrollView的頂部下拉和底部上拉回彈效果
本篇文章主要介紹了Android ScrollView的頂部下拉和底部上拉回彈效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
這篇文章主要為大家詳細(xì)介紹了Android SwipeRefreshLayout仿抖音app靜態(tài)刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2016-11-11
Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android獲取熱點(diǎn)主機(jī)ip和連接熱點(diǎn)手機(jī)ip的代碼
這篇文章主要介紹了Android獲取熱點(diǎn)主機(jī)ip和連接熱點(diǎn)手機(jī)ip的相關(guān)資料,需要的朋友可以參考下2018-01-01

