spinner的两种创建方式
spinner是安卓中一个常见的控件,它的中文含义是下拉列表的意思,它有两种创建方式:
第一种:在布局中设置
1,在res文件夹下的values中的strings.xml中添加一个string类型的数组,形式如下:
<span style="font-family:KaiTi_GB2312;font-size:24px;"><resources>
<string name="app_name">spinner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="cities">
<item>北京</item>
<item>南京</item>
<item>上海</item>
<item>深圳</item>
<item>浙江</item>
<item>天津</item>
</string-array>
</resources></span>
2,在布局xml文件中添加spinner控件
<span style="font-family:KaiTi_GB2312;font-size:24px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择您所在的城市"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spinner"
</span><pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:24px;"> android:entries="@array/cities"</span>
android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
3,java代码
<span style="font-family:KaiTi_GB2312;font-size:24px;">Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = ArrayAdapter
.createFromResource(this, R.array.photo_from,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.
R.layout.simple_spinner_dropdown_item);
</span><pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:24px;">spinner .setAdapter(adapter);</span>
第二种:在java代码中动态添加
1,在布局中添加一个textView和spinner控件
<span style="font-family:KaiTi_GB2312;font-size:24px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout></span>
2,在java代码中实现逻辑
<span style="font-family:KaiTi_GB2312;font-size:24px;">public class MainActivity extends Activity {
private static final String cities[]={"北京","上海","天津","广州","深圳"};
private TextView textview;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textView);
spinner=(Spinner)findViewById(R.id.spinner);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cities);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
textview.setText("您选择的城市是:"+cities[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
</span>
注意:这里的
<span style="font-family:KaiTi_GB2312;font-size:24px;"> android.R.layout.simple_spinner_item</span>
和
<span style="font-family:KaiTi_GB2312;font-size:24px;">android.R.layout.simple_spinner_dropdown_item</span>
是系统封装好的布局,不用自己添加,所以前缀必须加android