Spinner的创建、删除项、添加项

在layout目录下新建一个xml文件,名字随便(我这里叫myspinner.xml)。在这个文件里面可以定义下拉菜单的样式

我们这里采用TextView来实现

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:Android="http://schemas.android.com/apk/res/android"
Android:id="@+id/text1"
Android:layout_width="wrap_content"
Android:layout_height="24sp"
Android:singleLine="true"
style="?Android:attr/spinnerDropDownItemStyle" />

在Activity中我们可以这样调用
private static final String[] countriesStr={"","","",""}
mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
mySpinner.setAdapter(adapter);

利用自定义的xml我们就可以很灵活的来改变下拉菜单的样式。

另外andorid也提供了两种基本的样式
Android.R.layout.simple_spinner_item:TextView的下拉菜单
Android.R.layout.simple_spinner_dropdown_item:右边带有radio的下拉菜单

============

方法2:

自定义下拉菜单 -------- Spinner
import java.util.ArrayList;
import java.util.List;
import Android.app.Activity;
import Android.os.Bundle;
import Android.view.MotionEvent;
import Android.view.View;
import Android.view.animation.Animation;
import Android.view.animation.AnimationUtils;
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 SpinnerDrop extends Activity {
/** Called when the activity is first created. */
private List cityList;
private Spinner sp;
private int pos;
//private Animation animation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cityList= new ArrayList();//初始化一个city的String arraylist
countryList.add("长沙");
countryList.add("北京");
countryList.add("邵阳");
pos=0;//标记选择的位置
sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,Android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);//通过setAdapter()来读取ArrayAdapter里的数据
sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){//////下拉列表的选择事件
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
pos = position;//////////////标记选择的位置
TextView tv = (TextView)findViewById(R.id.xs);
tv.setText("selected:"+cityList.get(position).toString());//显示选择的内容
parent.setVisibility(View.VISIBLE);////设置下拉列表的显示状态
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

});

Button add = (Button)findViewById(R.id.add);//添加一个下拉项
add.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et = (EditText)findViewById(R.id.EditText01);
String newstr = et.getText().toString();
cityList.add(newstr);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerDrop.this,Android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);//添加数据后重新设置ArrayAdapter并加载进去
et.setText("");
Toast.makeText(SpinnerDrop.this, "添加成功", Toast.LENGTH_LONG).show();//浮框提示添加成功
}

});
Button del = (Button)findViewById(R.id.del);//删除下拉项
del.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
cityList.remove(pos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerDrop.this,Android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);
Toast.makeText(SpinnerDrop.this, "删除成功", Toast.LENGTH_LONG).show();
}

});

}
}


本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-01/31452.htm

你可能感兴趣的:(android,String,layout,dropdown,animation,button)