import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerTest extends Activity {
private LinearLayout mainLayout=null;
private TextView tv1=null;
private Spinner spinner1=null;
private TextView tv2=null;
private Spinner spinner2=null;
private TextView tv3=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout_init();
setContentView(mainLayout);
}
void mainLayout_init(){
mainLayout=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);
mainLayout.setLayoutParams(lp);
mainLayout.setOrientation(LinearLayout.VERTICAL);
tv1_init();
mainLayout.addView(tv1);
spinner1_init();
mainLayout.addView(spinner1);
tv2_init();
mainLayout.addView(tv2);
spinner2_init();
mainLayout.addView(spinner2);
tv3=new TextView(this);
tv3.setText("显示");
mainLayout.addView(tv3);
}
/*tv1初始化*/
void tv1_init(){
tv1=new TextView(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
tv1.setLayoutParams(lp);
tv1.setText("Spinner测试");
}
/*spinner初始化*/
void spinner1_init(){
spinner1=new Spinner(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);
spinner1.setLayoutParams(lp);
spinner1.setPrompt("这是标题");
String s[]={"选项一","选项二","选项三"};
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,s);
//设置为下拉式Item格式
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
//添加监听
OnItemSelectedListener otsc=new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
tv3.setText("选项"+String.valueOf(arg2+1)+"被你选中了");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
};
spinner1.setOnItemSelectedListener(otsc);
}
/**/
void tv2_init(){
tv2=new TextView(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
tv2.setLayoutParams(lp);
tv2.setText("换一种风格");
}
/**/
void spinner2_init(){
//
spinner2=new Spinner(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);
spinner2.setLayoutParams(lp);
//spinner2.setPrompt("这是标题");
String s[]={"选项一","选项二","选项三"};
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,s);
//另一种风格
aa.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner2.setAdapter(aa);
}
}