本实例是通过在编辑框内输入新的Spinner项目,然后点击删除和添加按钮动态改变Spinner的实例。
下面是具体代码:
xml文件中代码:
<?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/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="20sp" />
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="添 加"
android:textSize="20sp" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删 除"
android:textSize="20sp" />
</LinearLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
主程序Activity类中的代码:
package com.example.androidnetworkprogram;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class DaynamicSpinner extends Activity {
private TextView mTextView = null;
private EditText mEditText = null;
private Button addButton = null;
private Button removeButton = null;
private Spinner mySpinner = null;
private String[] spinnerStrArr = null;
private ArrayAdapter<String> myAdapter = null;
private List<String> myList = null;
private AlertDialog.Builder myAlertDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamicspinner);
mySpinner = (Spinner)this.findViewById(R.id.spinner1);
mTextView = (TextView)this.findViewById(R.id.textview1);
mEditText = (EditText)this.findViewById(R.id.edittext1);
addButton = (Button)this.findViewById(R.id.button11);
removeButton = (Button)this.findViewById(R.id.button12);
spinnerStrArr = getResources().getStringArray(R.array.application);
myList = new ArrayList<String>();
for(int i=0;i<spinnerStrArr.length;i++){
myList.add(spinnerStrArr[i]);
}
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,myList);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
myAlertDialog = new AlertDialog.Builder(DaynamicSpinner.this);
myAlertDialog.setTitle("警告!!!");
myAlertDialog.setMessage("请填入信息啊");
myAlertDialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
myAlertDialog.setNegativeButton("取消", null);
addListenerToButton();
}
public void addListenerToButton(){
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String tempStr = mEditText.getText().toString().trim();
if(!tempStr.equals("")){
myAdapter.add(tempStr);
int position = myAdapter.getPosition(tempStr);
mySpinner.setSelection(position);
}
else{
myAlertDialog.show();
}
}
});
removeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String tempStr = mEditText.getText().toString().trim();
if(myAdapter.getPosition(tempStr)>=0){
myAdapter.remove(tempStr);
int position = myAdapter.getPosition(tempStr);
mySpinner.setSelection(position);
}
else{
myAlertDialog.setMessage("你输入的信息不存在");
myAlertDialog.show();
}
}
});
}
}
资源数组xml中的代码(放在res/value/string.xml文件中)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">androidnetworkprogram</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="application">
<item>genymotion</item>
<item>eclipse</item>
<item>matlab</item>
<item>visual studio</item>
<item>VC 6.0++</item>
<item>Tencent QQ</item>
</string-array>
</resources>