源代碼以下,配置信息參看下載文件
package com.yanjun;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public
class MainActivity
extends Activity {
private
static
final String[] CONTENT = {
"北京",
"上海",
"山東",
"四川",
"雲南",
"河北",
"河南",
"黑龍江" };
TextView textView;
ArrayAdapter<String> adapter;
Spinner spinner;
// 動畫效果
Animation animation;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView1);
spinner = (Spinner) findViewById(R.id.spinner1);
// 適配內容
adapter =
new ArrayAdapter<String>(MainActivity.
this,
android.R.layout.simple_spinner_item, CONTENT);
// 獲取模板
adapter.setDropDownViewResource(R.layout.moban);
// 將ArrayAdapter添加到spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new Spinner.OnItemSelectedListener() {
public
void onItemSelected(AdapterView<?> parent, View view,
int position,
long id) {
textView.setText(
"您選擇的是" + CONTENT[position]);
// 將spinner顯示
parent.setVisibility(View.VISIBLE);
}
public
void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
// 設置動畫效果
animation = AnimationUtils.loadAnimation(
this, R.anim.anim);
// 將spinner添加OnTouchListener
spinner.setOnTouchListener(
new OnTouchListener() {
public
boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.startAnimation(animation);
v.setVisibility(View.VISIBLE);
return
false;
}
});
}
}
該圖代碼以下
package com.yanjun;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public
class MainActivity
extends Activity {
// 原spinner信息
private
static
final String[] CONTENT = {
"北京",
"上海" };
// 適配器
ArrayAdapter<String> adapter;
// 下拉控件
Spinner spinner;
// 按鈕控件
Button addButton, deleteButton;
// 輸入控件
EditText editText;
// 內容列表
List<String> allContent;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 內容列表部分
allContent =
new ArrayList<String>();
for (
int i = 0; i < CONTENT.length; i++) {
// 自動添加到內容的末尾
allContent.add(CONTENT[i]);
}
// new ArrayAdapter對象並將allContent傳入
adapter =
new ArrayAdapter<String>(
this, R.layout.moban, allContent);
// 得到系統自帶的佈局文件 android.R.layout.simple_dropdown_item_1line
//adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//帶有radio選項的列表
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addButton = (Button) findViewById(R.id.button_add);
deleteButton = (Button) findViewById(R.id.button_delete);
editText = (EditText) findViewById(R.id.editText1);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
addButton.setOnClickListener(
new OnClickListener() {
public
void onClick(View v) {
// 先比較添加的值是否存在,不存在再添加
String addString = editText.getText().toString();
for (
int i = 0; i < adapter.getCount(); i++) {
if (addString.equals(adapter.getItem(i))) {
Toast.makeText(MainActivity.
this,
"內容已經存在", 0).show();
editText.setText("");
return;
}
}
if (!addString.equals("")) {
//將之添加到adapter
adapter.add(addString);
//取得添加值的位置
int position = adapter.getPosition(addString);
//將spinner選擇在添加的位置上
spinner.setSelection(position);
editText.setText("");
}
}
});
deleteButton.setOnClickListener(
new OnClickListener() {
public
void onClick(View v) {
// 在不爲空的狀況下,刪除所選內容
if (spinner.getSelectedItem()!=
null) {
adapter.remove(spinner.getSelectedItem().toString());
editText.setText("");
if (adapter.getCount()==0) {
editText.setText("");
}
}
}
});
//spinner監聽事件
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public
void onItemSelected(AdapterView<?> parent, View view,
int position,
long id) {
// 點擊選中項在,並在edittext上顯示
editText.setText(parent.getSelectedItem().toString());
}
public
void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub } }); } }