《老罗Android》学习之UI控件Button

1.  Button
 实例1:按钮的焦点变化

1. 布局两个按钮
 <Button  android:background="@drawable/button1">  这样,按钮上就有图案了。
2. Activity.java 
public class MainActivity extends Activity implements OnClickListener, OnTouchListener,
OnFocusChangeListener, OnKeyListener {
	private int value = 1;// 用于改变按钮的大小
	private Button commonButton;
	private Button imageButton;
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	commonButton = (Button) this.findViewById(R.id.commonbutton);
	imageButton = (Button) this.findViewById(R.id.imagebutton);
	commonButton.setOnClickListener(this);
	imageButton.setOnClickListener(this);
	imageButton.setOnTouchListener(this);
	imageButton.setOnFocusChangeListener(this);
	imageButton.setOnKeyListener(this);
}
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);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
	if (KeyEvent.ACTION_DOWN == event.getAction()) {
		v.setBackgroundResource(R.drawable.button3);
	} else if (KeyEvent.ACTION_UP == event.getAction()) {
		v.setBackgroundResource(R.drawable.button2);
	}
	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;
	}
}
这里实现了4种监听方法。
实例2:图文混排的按钮
1.布局实现
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="30dp"
android:drawableRight="@drawable/star"
android:drawableTop="@drawable/star"
android:text="按钮4" >
2. 代码实现
Button button = (Button) this.findViewById(R.id.button);
SpannableString spannableStringLeft = new SpannableString("left");
Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(),R.drawable.image_left);
ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft,DynamicDrawableSpan.ALIGN_BOTTOM);
spannableStringLeft.setSpan(imageSpanLeft, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString spannableStringRight = new SpannableString("right");
Bitmap bitmapRight = BitmapFactory.decodeResource(getResources(),R.drawable.image_right);
ImageSpan imageSpanRight = new ImageSpan(bitmapRight);
spannableStringRight.setSpan(imageSpanRight, 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
button.append(spannableStringLeft);
button.append("我的按钮");
button.append(spannableStringRight);
 

2. RadioButton

1. 布局RadioButton要加在RadioGroup中。

 <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>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" ></RadioButton>
 </RadioGroup>
2. Activity.java中
group = (RadioGroup) this.findViewById(R.id.sex);
	button = (Button) this.findViewById(R.id.button);
	button.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {			
	  int len = group.getChildCount();// 获得单选按钮组的选项个数
	  String msgString = "";
	for (int i = 0; i < len; i++) {
		RadioButton radioButton = (RadioButton) group.getChildAt(i);
		if (radioButton.isChecked()) {
		msgString = radioButton.getText().toString();
		break;
		}
		}}
});
3. ToggleButton 开关按钮
1. 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
  <ToggleButton android:id="@+id/toggleButton1"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:checked="true" android:textOff="横向排列" android:textOn="纵向排列" />
	<LinearLayout android:id="@+id/lLayout"
		android:layout_width="match_parent" android:layout_height="wrap_content"
		android:orientation="vertical">
		<Button android:id="@+id/button1" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Button" />
		<Button android:id="@+id/button2" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Button" />
		<Button android:id="@+id/button3" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="Button" />
	</LinearLayout>
</LinearLayout>
2. MainActivity.java
ToggleButton toggle=(ToggleButton)findViewById(R.id.toggleButton1);
        final LinearLayout layout=(LinearLayout)findViewById(R.id.lLayout);
        toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {             
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if(arg1){                   
                    layout.setOrientation(1);//设置垂直布局
                }else{
                    
                    layout.setOrientation(0);//设置水平布局
                }                 
            }
      });
开关按钮有一个checked属性表示是否选中,可以用在网络关闭等地方。这里实现按一下,转变布局方向的功能。
4. CheckBox 复选框
1. 这里采用动态创建的方式,要先设定一个布局,checkbox.xml
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:id="@+id/checkbox"
	android:layout_height="wrap_content">
</CheckBox>
2. MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
	private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String[] checkboxText = new String[] { "您是学生吗?", "是否喜欢android?",
				"您喜欢旅游吗?", "打算出国吗?" };
		// 动态加载布局
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.activity_main, null);
		// 给指定的checkbox赋值
		for (int i = 0; i < checkboxText.length; i++) {
			// 先获得checkbox.xml的对象
			CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);
			checkBoxs.add(checkBox);
			checkBoxs.get(i).setText(checkboxText[i]);
			// 实现了在main布局中添加checkbox
			linearLayout.addView(checkBox, i);
		}
		setContentView(linearLayout);
		Button button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(this);		
	}
	@Override
	public void onClick(View v) {
		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();
	}
}
这里使用一个对话框来提示信息。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值