Android入门之Spinner(纯java)

   

 

  

 

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);
    }
}


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值