android————Button

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/seektext1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        
        />
    <TextView android:id="@+id/seektext2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        
        />

    <SeekBar 
        android:id="@+id/seekbar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        />
    <SeekBar 
        android:id="@+id/seekbar2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60"
        />
</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button android:id="@+id/btn_chang1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的按钮一"
        />
    <Button android:id="@+id/btn_chang2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的按钮二"
        android:background="@drawable/button1" 
        />

</LinearLayout>

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

	
	
	
}

下面是在按钮上加图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/face1"
            android:text="按钮1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawablePadding="30dp"
            android:drawableTop="@drawable/face2"
            android:text="按钮2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/face2"
            android:drawableTop="@drawable/face2"
            android:text="按钮3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawablePadding="30dp"
            android:drawableRight="@drawable/face2"
            android:text="按钮4" />
          
    </LinearLayout>

     <Button
        android:id="@+id/btn_imgbtn"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="10dp" 
        />

</LinearLayout>

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

}

单选按钮的使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="match_parent"
    android:layout_height="wrap_content" android:text="性别:"/>
    <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="男"
            />
        
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="女"
            />
        
    </RadioGroup>
    <Button android:id="@+id/radbutton"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
      android:text="选择性别"
        />

</LinearLayout>

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<len;i++){
				android.widget.RadioButton radioButton=(android.widget.RadioButton) radioGroup.getChildAt(i);
				if(radioButton.isChecked()){
					msgString=radioButton.getText().toString();
					break;
				}
			}
				Toast.makeText(RadioButton.this, msgString, 1).show();
			}
		});
		
	}
}


动态多选按钮;;;;;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/checkbox"
    >
    

</CheckBox>

public class CheckActivity extends Activity implements OnClickListener {
	List<CheckBox> checkBoxs=new ArrayList<CheckBox>();
	@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<checkboxText.length;i++){
			CheckBox checkBox=(CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);
			checkBoxs.add(checkBox);
			checkBoxs.get(i).setText(checkboxText[i]);
			linearLayout.addView(checkBox,i);
		}
		//加载main的link
		setContentView(linearLayout);
		Button button=(Button) findViewById(R.id.btn_check);
	    button.setOnClickListener(this);
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String s="";
		for(CheckBox checkBox:checkBoxs){
			if(checkBox.isChecked()){
				s+=checkBox.getText()+"\n";
				
			}
			
		}
		if("".equals(s)){
			s="您还没有选择选项";
		}
		new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();
		
	}

}
最后一个是开关按钮,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    
    >

    <ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textOff="横向排列"
        android:textOn="纵向排列" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:id="@+id/linear"
        >
      <Button android:id="@+id/tbutton1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="button"
          />  
      <Button android:id="@+id/tbutton2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="button"
          />  
      <Button android:id="@+id/tbutton3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="button"
          />  
        
    </LinearLayout>

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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值