Android组件——Spinner

Spinner有些类似select组件,是单选的。系统默认效果图为:

Android组件——Spinner

代码如下:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Spinner sp = (Spinner) findViewById(R.id.sp1);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.team, android.R.layout.simple_spinner_dropdown_item);
		sp.setAdapter(adapter);
	}
}

 其中R.array.team所对应的资源文件放在res/values,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="team">
		<item>Boston</item>
		<item>Houston</item>
		<item>Phoenix</item>
		<item>Dallas</item>
		<item>L.A. Lakers</item>
		<item>Miami</item>
	</string-array>
</resources>

 main.xml布局文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Spinner android:id="@+id/sp1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:prompt="@string/title"
	/>
</LinearLayout>
 很简单的……如果要得到选择的值什么的还需继续研究。

你可能感兴趣的:(android,xml,OS)