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[]{"北京","上海","香港","澳门"}; ArrayAdapteradapter=new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item, city); spinner1.setAdapter(adapter); }}
/SpinnerDemo/res/layout/activity01.xml
1 26 7 12 13
三、注意点
1、android.R.layout.simple_spinner_dropdown_item
2、String[] city=new String[]{"北京","上海","香港","澳门"}; 字符串使用