<!-- String.xml -->
<resources>
<string name="app_name">My Application</string>
<string-array name="citys">
<item>石家庄</item>
<item>承德市</item>
<item>唐山市</item>
<item>秦皇岛市</item>
<item>沧州市</item>
<item></item>
</string-array>
</resources>
// MainActivity.java
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
/**
*
* @param parent
* @param view
* @param position:
* 被点击哪个
* @param id
*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String[] datas = getResources().getStringArray(R.array.citys);
Toast.makeText(MainActivity.this,datas[position], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(MainActivity.this,"您来自火星", Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(itemSelectedListener);
}
}
网友评论