Spinner使用一 一、使用方法 1、在layout中創建Spinner控制項 <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" / ...
Spinner使用一
一、使用方法
1、在layout中創建Spinner控制項
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
2、給數據適配器添加數據源和顯示格式
String[] city=new String[]{"北京","上海","香港","澳門"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, city);
3、給Spinner控制項添加數據適配器
spinner1.setAdapter(adapter);
二、效果圖及代碼
fry.Activity01
package fry; import com.example.SpinnerDemo.R; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class Activity01 extends Activity{ private Spinner spinner1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); spinner1=(Spinner) findViewById(R.id.spinner1); initSpinner1(); } public void initSpinner1(){ String[] city=new String[]{"北京","上海","香港","澳門"}; ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, city); spinner1.setAdapter(adapter); } }
/SpinnerDemo/res/layout/activity01.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="match_parent" 5 android:orientation="vertical" > 6 7 <Spinner 8 android:id="@+id/spinner1" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 /> 12 13 </LinearLayout>
三、註意點
1、android.R.layout.simple_spinner_dropdown_item
2、String[] city=new String[]{"北京","上海","香港","澳門"}; 字元串使用