下拉列表框Spinner
layout文件
item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="horizontal" > 6 7 <TextView 8 android:id="@+id/tv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 <ImageView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:src="@drawable/ic_launcher" /> 16 17 </LinearLayout>
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="@drawable/btn_media_player_disabled" 6 android:orientation="vertical" > 7 8 <Spinner 9 android:id="@+id/sp" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" /> 12 13 <AutoCompleteTextView 14 android:id="@+id/actv" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:completionThreshold="1" > 18 </AutoCompleteTextView> 19 20 </LinearLayout>
Activity
1 package cn.itcast.ui; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.AdapterView; 7 import android.widget.AdapterView.OnItemClickListener; 8 import android.widget.AdapterView.OnItemSelectedListener; 9 import android.widget.ArrayAdapter; 10 import android.widget.AutoCompleteTextView; 11 import android.widget.Spinner; 12 13 public class UiActivity extends Activity { 14 private Spinner spinner; 15 private AutoCompleteTextView actv; 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 spinner = (Spinner) findViewById(R.id.sp); 21 actv = (AutoCompleteTextView)findViewById(R.id.actv); 22 String[] names = {"老张", "老方", "老毕", "李明" , "李丽", "陈江", "abc", "acc"}; 23 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names); 24 actv.setAdapter(adapter); 25 26 27 spinner.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.tv, new String[]{"java",".net","php"})); 28 29 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 30 31 @Override 32 public void onItemSelected(AdapterView<?> parent, View view, 33 int position, long id) { 34 35 } 36 37 @Override 38 public void onNothingSelected(AdapterView<?> parent) { 39 40 } 41 }); 42 43 } 44 }
基础/day08/ui