package com.turboradio.googlesdk;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Ex04_09_Spinner extends Activity {
private static final String [] countriesStr = {
"北京市","上海市","天津市","重庆市"
};
private TextView myTextView;
private EditText myEditText;
private Spinner mySpinner;
private ArrayAdapter<String> adapter;
private List<String> allCountries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_04_09);
allCountries = new ArrayList<String>();
for(int i = 0;i<countriesStr.length;i++){
allCountries.add(countriesStr[i]);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allCountries);
/**设置Item的样式**/
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
myTextView = (TextView)findViewById(R.id.text1);
myEditText = (EditText)findViewById(R.id.edit);
mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myTextView.setText(parent.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
/**
* 添加按钮事件
*/
public void addSpinnerItem(View v){
String newCountry = myEditText.getText().toString();
/**先判断值是否存在,不存在再添加**/
for(int i = 0;i<adapter.getCount();i++){
if(newCountry.equals(adapter.getItem(i))){
return;
}
}
if(!newCountry.equals("")){
adapter.add(newCountry);
int position = adapter.getPosition(newCountry);
mySpinner.setSelection(position);
myEditText.setText("");
}
}
/**
* 删除按钮事件
*/
public void removeSpinnerItem(View v){
if(mySpinner.getSelectedItem() != null){
/**删除mySpinner的值**/
adapter.remove(mySpinner.getSelectedItem().toString());
myEditText.setText("");
if(adapter.getCount() == 0){
myTextView.setText("");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="addSpinnerItem"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="removeSpinnerItem"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>