使用ArrayList动态数组的依赖性实现动态增减Spinner下拉菜单选项功能。
设置一个EditText输入框,当用户输入了文字并单击【添加】按钮的同时,就会将输入的值添加Spinner至下拉菜单的最后一项,接着Spinner会停留在刚刚添加的选项上,单击【删除】按钮则会删除选择的Spinner选项。
运行截图:
程序结构
package com.example.asus.gary_0400; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private static final String[] countriesStr = {"周一","周二","周三","周四","周五"};
private TextView myTextView;
private EditText myEditText;
private Button myButton_add;
private Button myButton_remove;
private Spinner mySpinner;
private ArrayAdapter<String>adapter;
private List<String> allCountries; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allCountries = new ArrayList<String>(); for(int i=0;i<countriesStr.length;i++)
{
allCountries.add(countriesStr[i]);
} //新建ArrayAdapter对象并将allCountries传入
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,allCountries); adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item); //以findViewById()取的对象
myTextView = (TextView)findViewById(R.id.myTextView);
myEditText = (EditText)findViewById(R.id.myEditText);
myButton_add=(Button)findViewById(R.id.myButton_add);
myButton_remove = (Button)findViewById(R.id.myButton_remove);
mySpinner = (Spinner)findViewById(R.id.myspinner); //将ArrayAdapter添加到Spinner对象中
mySpinner.setAdapter(adapter); myButton_add.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(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
adapter.add(newCountry);
//取得添加的值的位置
int position = adapter.getPosition(newCountry);
//将Spinner选择在添加的值的位置
mySpinner.setSelection(position);
//将myEditText清空
myEditText.setText("");
}
}
}); //为myButton_remove添加OnclickListener
myButton_remove.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if(mySpinner.getSelectedItem()!=null)
{
//删除mySpinner的值
adapter.remove(mySpinner.getSelectedItem().toString());
//将myEditText清空
myEditText.setText("");
if(adapter.getCount()==0)
{
//将myTextView清空
myTextView.setText("");
}
}
}
}); //为mySpinner添加OnItemSelextedListener
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//将所选mySpinner的值带入myTextView中
myTextView.setText(parent.getSelectedItem().toString());
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
});
}
}
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.asus.gary_0400.MainActivity"
android:weightSum="1"> <TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gary_动态添加或删除菜单"
android:textSize="40px" /> <EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:hint="填写添加项"
android:inputType="textPersonName" /> <Button
android:id="@+id/myButton_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" /> <Button
android:id="@+id/myButton_remove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除" /> <Spinner
android:id="@+id/myspinner"
android:layout_width="match_parent"
android:layout_height="50px"
android:layout_weight="0.13" /> </LinearLayout>
activity_main.xml
一、界面布局
一个TextView显示框用来显示添加所选项的字符串, EditText作为输入框,两个Button按钮作为”添加“和“删除”对Spinner菜单进行操作
二、实现程序功能
1、创建ArrayAdapter对象并将当前Spinner菜单中数据个数多少的值传入
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,allCountries); adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
2、将ArrayAdapter添加到Spinner对象中
// mySpinner = (Spinner)findViewById(R.id.myspinner);
//mySpinner取得Spinner控件对象
mySpinner.setAdapter(adapter);
3、实现"添加"按钮功能
myButton_add.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(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
adapter.add(newCountry);
//取得添加的值的位置
int position = adapter.getPosition(newCountry);
//将Spinner选择在添加的值的位置
mySpinner.setSelection(position);
//将myEditText清空
myEditText.setText("");
}
}
});
4、实现"删除"按钮功能
myButton_remove.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if(mySpinner.getSelectedItem()!=null)
{
//删除mySpinner的值
adapter.remove(mySpinner.getSelectedItem().toString());
//将myEditText清空
myEditText.setText("");
if(adapter.getCount()==0)
{
//将myTextView清空
myTextView.setText("");
}
}
}
});
5、实现TextView控件功能(显示添加进入Spinner中的字符串)
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//将所选mySpinner的值带入myTextView中
myTextView.setText(parent.getSelectedItem().toString());
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
});