android————Button

首先我们介绍一下SeekBar,即使拖动滑条,比如控制声音



    
    
    

    
    


public class SeekBarActivity extends Activity implements OnSeekBarChangeListener{

	private TextView textView1,textView2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.seekbar);
		textView1=(TextView) findViewById(R.id.seektext1);
		textView2=(TextView) findViewById(R.id.seektext2);
	    
		SeekBar seekBar1=(SeekBar) findViewById(R.id.seekbar1);
	    SeekBar seekBar2=(SeekBar) findViewById(R.id.seekbar2);
	    seekBar1.setOnSeekBarChangeListener(this);
	    seekBar2.setOnSeekBarChangeListener(this);
	    
	}

	//当滑动滑竿是触发的事件
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		if(seekBar.getId()==R.id.seekbar1){
			textView1.setText("seekBar1的当前位置是:"+progress);
			
		}else{
			textView2.setText("seekBar2的当前位置是:"+progress);
		}
		
	}

	//表示从哪里开始拖动
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				if(seekBar.getId()==R.id.seekbar1){
					textView1.setText("seekBar1的开始拖动");
					
				}else{
					textView2.setText("seekBar2的开始拖动");
				}
	}

	//表示从哪里结束拖动
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		if(seekBar.getId()==R.id.seekbar1){
			textView1.setText("seekBar1的停止拖动");
			
		}else{
			textView2.setText("seekBar2的停止拖动");
		}
}
	

}

下面是button的各种按键事件: OnClickListener,OnFocusChangeListener,OnTouchListener,OnKeyListener



    

public class ButtonChanage extends Activity implements OnClickListener,OnFocusChangeListener,OnTouchListener,OnKeyListener{
	private Button commonButton;
	private Button imageButton;
	private int value=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buttonchage);
		commonButton=(Button) findViewById(R.id.btn_chang1);
		imageButton=(Button) findViewById(R.id.btn_chang2);
		//单击按下的效果
		commonButton.setOnClickListener(this);
		imageButton.setOnClickListener(this);
		//显示屏触摸的动作
		imageButton.setOnTouchListener(this);
		//焦点变化
		imageButton.setOnFocusChangeListener(this);
		//按钮按上或者按下
		imageButton.setOnKeyListener(this);
		
		
	}
	@Override
	public void onClick(View v) {
		Button button=(Button) v;
		if(value==1&&button.getWidth()==getWindowManager().getDefaultDisplay().getWidth()){
			value=-1;
		}else if(value==-1&&button.getWidth()<100){
			value=1;
		}
		button.setWidth(button.getWidth()+(int)(button.getWidth()*0.1)*value);
		button.setHeight(button.getHeight()+(int)(button.getHeight()*0.1)*value);
		
		}
	@Override
	public boolean onKey(View v, int arg1, KeyEvent event) {
	 
		if(KeyEvent.ACTION_DOWN==event.getAction()){
			v.setBackgroundResource(R.drawable.button2);
		}else if(KeyEvent.ACTION_UP==event.getAction()){
			v.setBackgroundResource(R.drawable.button3);
		}
		return false;
	}
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if(hasFocus){
			imageButton.setBackgroundResource(R.drawable.button2);
		}else{
			imageButton.setBackgroundResource(R.drawable.button1);
		}
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_UP){
			v.setBackgroundResource(R.drawable.button1);
			
		}else if(event.getAction()==MotionEvent.ACTION_DOWN){
			v.setBackgroundResource(R.drawable.button2);
		}
		return false;
	}

	
	
	
}

下面是在按钮上加图片




    

        

public class ButtonImage extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buttoniamge);

		Button button = (Button) findViewById(R.id.btn_imgbtn);
		
		SpannableString spannedStringLeft = new SpannableString("left");
		Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(),
				R.drawable.face4);
		ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft,
				DynamicDrawableSpan.ALIGN_BOTTOM);
		spannedStringLeft.setSpan(imageSpanLeft, 0, 4,
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		

		SpannableString spannableStringRight = new SpannableString("right");
		Bitmap bitmapright = BitmapFactory.decodeResource(getResources(),
				R.drawable.face1);
		ImageSpan imageRight = new ImageSpan(bitmapright);
		spannableStringRight.setSpan(imageRight, 0, 5,
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		button.append(spannedStringLeft);
		button.append("我的按钮");
		button.append(spannableStringRight);
	}

}

单选按钮的使用



    
    
        
        
        
        
    
    

public class RadioButton extends Activity {

	private RadioGroup radioGroup;
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radiobutton);
		button=(Button) findViewById(R.id.radbutton);
		radioGroup=(RadioGroup) findViewById(R.id.sex);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			int len=radioGroup.getChildCount();
			String msgString="";
			for(int i=0;i


动态多选按钮;;;;;




    



    



public class CheckActivity extends Activity implements OnClickListener {
	List checkBoxs=new ArrayList();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.check);
		String[] checkboxText=new String[]{"你是中国人吗?","你是美国人吗?","你喜欢旅游吗?","你是程序员吗?"};
		// 动态加载布局
		LinearLayout linearLayout=(LinearLayout) getLayoutInflater().inflate(R.layout.check, null);
		
		for(int i=0;i
最后一个是开关按钮,




    

    
      
public class KaiGuanActivity extends Activity {

	private ToggleButton button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.kg);
		button=(ToggleButton) findViewById(R.id.togglebutton);
		final LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linear);
	    button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					linearLayout.setOrientation(1);
				}else
				{
					linearLayout.setOrientation(0);
				}
			}
		});
	
	}
}






你可能感兴趣的:(android)