欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Spinner 組件的應(yīng)用實例

 更新時間:2017年09月01日 10:55:37   投稿:lqh  
這篇文章主要介紹了Android Spinner 組件的應(yīng)用實例的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下

Android Spinner 組件

Spinner: 下拉組件

使用事項:布局在XML 中實現(xiàn),具體的數(shù)據(jù)在JAVA 代碼中實現(xià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 實體對象 , 用于顯示星座的詳細(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"); 
 
 
    /* 實現(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() { 
 
      /* 對所有的選擇項進(jìn)行監(jiān)控 *//* 參3:當(dāng)前選擇項的ID 參4:被選擇項在組中的位置,邏輯上與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 組件的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論