list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv" />
</RelativeLayout>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner" />
<Button android:layout_below="@+id/spinner" android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" android:onClick="doClick" />
<Button android:id="@+id/bt2" android:layout_below="@+id/spinner" android:layout_toRightOf="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/delete" android:onClick="doClick" />
</RelativeLayout>
代码实现:
package com.spinner;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
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 MainActivity extends Activity {
Spinner sp;
Button bt1,bt2;
List<String> al;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setView();
sp.setPrompt("你喜欢的动漫是:");
al=new ArrayList<String>();
al.add("海贼王");
al.add("火影忍者");
al.add("游戏王");
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, R.layout.list,R.id.tv, al);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItemAtPosition(position).toString(), 3000).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void setView() {
sp=(Spinner) findViewById(R.id.spinner);
bt1=(Button) findViewById(R.id.bt1);
bt2=(Button) findViewById(R.id.bt2);
}
public void doClick(View v){
et=new EditText(this);
et.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
switch(v.getId()){
case R.id.bt1:
new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star)
.setTitle("你要添加的内容:").setView(et)
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<al.size();i++){
if(al.get(i).equalsIgnoreCase(et.getText().toString())){
break;
}
}
al.add(et.getText().toString());
}
}).setNegativeButton("取消", null)
.create().show();
break;
case R.id.bt2:
new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star)
.setTitle("你要删除的内容:").setView(et)
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
al.remove(et.getText().toString());
}
}).setNegativeButton("取消", null)
.create().show();
break;
}
}
}