下拉列表 spinner(android)
下拉列表 spinner,这个使用很简单,在 xml 中定义一个标签,然后指定 ArrayAdapter<String> 就可以了,
Adapter 需要指定下拉的布局。关于下拉的数据,书中是写用静态数组,我这里改成了从资源文件中读取
java 代码
package zziss.android.spinnerTest;
import java.util.Map;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinnerTestActivity extends Activity {
/** Called when the activity is first created. */
private TextView iTv;
private Spinner iSpinner;
private String[] items;
private ArrayAdapter<Map<String,Integer>> iAda;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iTv = (TextView) this.findViewById(R.id.tv);
iSpinner = (Spinner) this.findViewById(R.id.spinner);
Resources res = getResources();
items = res.getStringArray(R.array.listItems);
ArrayAdapter<String> ada = new ArrayAdapter<String>( this,
android.R.layout.simple_spinner_item,items);
ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
iSpinner.setAdapter(ada);
iSpinner.setOnItemSelectedListener( new Spinner.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iTv.setText(items[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
import java.util.Map;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinnerTestActivity extends Activity {
/** Called when the activity is first created. */
private TextView iTv;
private Spinner iSpinner;
private String[] items;
private ArrayAdapter<Map<String,Integer>> iAda;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iTv = (TextView) this.findViewById(R.id.tv);
iSpinner = (Spinner) this.findViewById(R.id.spinner);
Resources res = getResources();
items = res.getStringArray(R.array.listItems);
ArrayAdapter<String> ada = new ArrayAdapter<String>( this,
android.R.layout.simple_spinner_item,items);
ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
iSpinner.setAdapter(ada);
iSpinner.setOnItemSelectedListener( new Spinner.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iTv.setText(items[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
main.xml
<?
xml version="1.0" encoding="utf-8"
?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >
< TextView
android:id ="@+id/tv"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="" />
< Spinner
android:id ="@+id/spinner"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerHorizontal ="true"
/>
</ LinearLayout >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >
< TextView
android:id ="@+id/tv"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="" />
< Spinner
android:id ="@+id/spinner"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerHorizontal ="true"
/>
</ LinearLayout >
string.xml
<?
xml version="1.0" encoding="utf-8"
?>
< resources >
< string name ="hello" >Hello World, SpinnerTestActivity! </ string >
< string name ="app_name" >SpinnerTest </ string >
< string-array name ="listItems" >
< item >A型 </ item >
< item >B型 </ item >
< item >AB型 </ item >
< item >O型 </ item >
</ string-array >
</ resources >
< resources >
< string name ="hello" >Hello World, SpinnerTestActivity! </ string >
< string name ="app_name" >SpinnerTest </ string >
< string-array name ="listItems" >
< item >A型 </ item >
< item >B型 </ item >
< item >AB型 </ item >
< item >O型 </ item >
</ string-array >
</ resources >