(1)代码区:
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.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {//注册点击监听
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, arg2+"", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
(2)xml文件:
在res / value下建立一个名为array类型为resources的文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="spingarrtest">
<item>美国</item>
<item>俄罗斯</item>
<item>中国</item>
<item>德国</item>
<item>日本</item>
</string-array>
</resources>
(3)布局文件代码:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/spingarrtest"
/>