|
动态添加╱删除的 Spinner 菜单
ArrayList 与 Widget 的依赖性
①创建新工程
②修改 mian.xml 布局,添加 UI 元素
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget92"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/TextView_Show_yourChoice"
android:layout_width="190px"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="25sp"
android:layout_gravity="center_horizontal"
>
</TextView>
<EditText
android:id="@+id/EditView_Input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<Button
android:id="@+id/Button_ADD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
>
</Button>
<Button
android:id="@+id/Button_DEL"
android:layout_width="fill_parent"
android:layout_height="wrap_content
android:text="删除"
>
</Button>
<Spinner
android:id="@+id/Spinner_Slecte"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</Spinner>
</LinearLayout>
复制代码
③定义创建一个 List<T>
/*定义*/
private String[] cities;
private List<String> cityList;
/*初始化字符串数组*/
cities = new String[] { "Android", "BlackBerry", "J2ME", "Symbian",
"Broncho", "LinuxMobile", "alm", "WindwosMobile" };
/*初始化List实例 ArrayList的对象*/
cityList = new ArrayList<String>();
/*遍历,把字符串数组添加到ArrayList中*/
for (int i = 0; i < cities.length; i++) {
cityList.add(cities);
}
复制代码④定义创建一个ArrayAdapter<String> /*定义*/
private ArrayAdapter<String> arrayAdapter;
/* 这里应该使用List<> ,如果使用String[]则会出错 */
/*初始化下拉菜单的内容适配器*/
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cityList);
复制代码⑤设置ArrayAdapter<String>在下拉菜单中的显示布局
/*设置下拉菜单显示内容的风格*/
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_
item);
复制代码⑥给下拉菜单添加内容 Adapter 适配器
/*给下拉菜单对象添加内容适配器*/
city_Spinner.setAdapter(arrayAdapter);
复制代码
⑦修改该mainActivity.java,实现动态添加和删除
package zyf.Ex_Ctrl_9ME;
/*使用的包导入*/
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Ex_Ctrl_9ME extends Activity implements Button.OnClickListener,
Spinner.OnItemSelectedListener {
/** Called when the activity is first created. */
/*声明程序使用的对象*/
private TextView show_yourChoice_TextView;
private EditText input_City_EditText;
private Button Add_Button, Del_Button;
private Spinner city_Spinner;
private ArrayAdapter<String> arrayAdapter;
private String[] cities;
private String addString;
private List<String> cityList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*设置主屏显示布局为main.xml*/
setContentView(R.layout.main);
/*findViewByID()获取XML中的UI元素*/
show_yourChoice_TextView =
(TextView) findViewById(R.id.TextView_Show_yourChoice);
input_City_EditText = (EditText) findViewById(R.id.EditView_Input);
Add_Button = (Button) findViewById(R.id.Button_ADD);
Del_Button = (Button) findViewById(R.id.Button_DEL);
city_Spinner = (Spinner) findViewById(R.id.Spinner_Slecte);
/*初始化字符串数组*/
cities = new String[] { "Android", "BlackBerry", "J2ME",
"Symbian","Broncho", "LinuxMobile",
"Palm", "WindwosMobile" };
/*初始化List实例 ArrayList的对象*/
cityList = new ArrayList<String>();
/*遍历,把字符串数组添加到ArrayList中*/
for (int i = 0; i < cities.length; i++) {
cityList.add(cities);
}
/* 这里应该使用List<> ,如果使用String[]则会出错 */
/*初始化下拉菜单的内容适配器*/
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cityList);
/*设置下拉菜单显示内容的风格*/
arrayAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/*给下拉菜单对象添加内容适配器*/
city_Spinner.setAdapter(arrayAdapter);
/*默认启动时 文本标题显示*/
show_yourChoice_TextView.setText(arrayAdapter.getItem(0));
/*默认启动时 下拉菜单第一项被选中*/
city_Spinner.setSelection(0);
/*为按钮添加点击事件监听器*/
Add_Button.setOnClickListener(this);
Del_Button.setOnClickListener(this);
/*为下拉菜单添加选项选中事件监听器*/
city_Spinner.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*区别按钮来进行不同动作*/
case R.id.Button_ADD:
/*显示Toast提示*/
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
/*获得输入框中的将要添加的字符串*/
addString = input_City_EditText.getText().toString();
/*遍历,比较是否和下拉菜单中内容重复*/
for (int i = 0; i < arrayAdapter.getCount(); i++) {
if (addString.equals(arrayAdapter.getItemId(i))) {
return;
/*重复了,则跳出*/
}
}
/*如果添加字符串不为""*/
if (!addString.equals("")) {
/*添加进适配器中*/
arrayAdapter.add(addString);
/*获取刚刚添加进的字符串位置*/
int post = arrayAdapter.getPosition(addString);
/*设置刚刚添加进的下拉菜单内容被选中*/
city_Spinner.setSelection(post);
/*清空输入框*/
input_City_EditText.setText("");
}
break;
case R.id.Button_DEL:
if (city_Spinner.getSelectedItem() != null) {
/* 删除mySpinner 的值 */
arrayAdapter.remove(city_Spinner.getSelectedItem().toString());
/* 将myEditText 清空 */
input_City_EditText.setText("");
if (arrayAdapter.getCount() == 0) {
/* 将myTextView 清空 */
Toast.makeText(this, "删完了", Toast.LENGTH_SHORT).show();
show_yourChoice_TextView.setText("");
}
}
break;
default:
break;
}
}
/*下拉菜单选项被选中事件处理*/
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
show_yourChoice_TextView.setText(arrayAdapter.getItem(arg2));
}
/*未被选中 事件处理*/
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
/*添加Menu菜单
进行退出操作*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("Exit");
return super.onCreateOptionsMenu(menu);
}
/*Menu菜单 退出*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
finish();
return super.onOptionsItemSelected(item);
}
}
复制代码结果: |
|