Android控件Spinner的使用方法(1)
一、使用方法
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
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
三、注意点
1、android.R.layout.simple_spinner_dropdown_item
2、String[] city=new String[]{"北京","上海","香港","澳门"}; 字符串使用
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)