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[]{"北京","上海","香港","澳门"}; 字符串使用
时间: 2024-11-07 04:48:37