spinner使用的5個步驟
1、在布局檔案中聲明Spinner控件
2、建立ArrayAdapter,
(1)在res/values/String.xml中聲明下拉菜單的數組,然後通過CreateFromResource()方法建立
(2)動态的在java代碼裡建立,調動ArrayAdapter的構造函數
3、在spinner上綁定adapter
4、寫個監聽器的類--實作OnItemSelectedListener接口
5、在spinner上綁定這個監聽器
具體代碼如下:
MainActivity.java
package com.example.s02_e01_spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.spinnerId);
//建立擴充卡有兩種方法:1是通過createFromResource方法建立ArrayAdapter對象,先 在res/values下的String.xml中聲明,這裡引用該資源;
//android.R.layout.simple_spinner_item android.R.layout.simple_dropdown_item_1line都是系統裡的樣式,也可以自定義
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.my_family,
android.R.layout.simple_spinner_item);//括号裡的參數詳見幫助文檔
//設定擴充卡的下拉樣式
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//2調用ArrayAdapter的構造函數動态的建立Arraydapter對象
//上面第一個參數:this,上下文對象;
//第2個參數:指定了下拉菜單中每一個條目的樣式(樣式風格寫在Relativelayout的屬性裡)
//第3個參數:指定了這個list是顯示在所在布局檔案中的某控件的Id。item.xml的布局檔案可有多個控件
//第4個參數:為整個清單提供的資料
//在spinner上添加上面劍豪的擴充卡
spinner.setAdapter(adapter);
//下面這條可有有個前提:activity_main.xml檔案中設定android:spinnerMode="dialog",這個屬性有兩個可選(dialog和dropdown)
spinner.setPrompt("成員");
//在spinner上綁定監聽器
spinner.setOnItemSelectedListener(new SpinnerOnItemSelectedListener());
}
class SpinnerOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> adapterview, View view, int postion,
long id) {
String selected=adapterview.getItemAtPosition(postion).toString();
System.out.println(selected);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
System.out.println("you have chosed nothing");
//這裡沒有看到效果???
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinnerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
/>
</RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">S02_E01_Spinner</string>
<string name="action_settings">Settings</string>
<string-array name="my_family">
<item >葫蘆娃</item>
<item >玉華</item>
<item >吉軒</item>
</string-array>
</resources>
item.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"
android:background="#ff0000"
>
<TextView
android:id="@+id/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
/>
</LinearLayout>