Spinner
<resources> <string name="app_name">HelloWorld</string> <string-array name="city_labels"> <item>北京</item> <item>上海</item> <item>广州</item> <item>深圳</item> </string-array> </resources>
<?xml version="1.0" encoding="utf-8"?> <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="com.pingyijinren.helloworld.MainActivity"> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/city_labels" android:id="@+id/spinner"></Spinner> </LinearLayout>
package com.pingyijinren.helloworld; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner=(Spinner)findViewById(R.id.spinner); spinner.setPrompt("选择你所在的城市"); spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String[] cities=getResources().getStringArray(R.array.city_labels); Toast.makeText(this,cities[position]+"",Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }