今天学习一下Spinner
<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" > <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/books" android:prompt="@string/tip" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/tip" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="books"> <item>java</item> <item>android</item> <item>web</item> </string-array> </resources>
package com.example.spinner; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends Activity { Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner=(Spinner) findViewById(R.id.spinner); String[] arr={"孙悟空","猪八戒","唐僧"}; ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr); spinner.setAdapter(adapter); } }