一、我们先使用ToggleButton(开关按钮)实现一个布局的横向和纵向排列。
布局文件如下:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view"
android:checked="true"
android:textOff="横向排列"
android:textOn="纵向排列" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toggle"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
</LinearLayout>
activity中的代码如下:
toggleButton = (ToggleButton) this.findViewById(R.id.toggle);
final LinearLayout layout = (LinearLayout) this
.findViewById(R.id.layout);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//通过检查是否被打开和关闭,用isChecked来判断。
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
layout.setOrientation(LinearLayout.VERTICAL);
} else {
layout.setOrientation(LinearLayout.HORIZONTAL);
}
}
});
二、用 radioGroup来选择性别。
布局文件:
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view2" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/man"
android:text="男" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女" />
</RadioGroup>
activity中实现代码,方法一:
final RadioButton manRadioButton = (RadioButton) this
.findViewById(R.id.man);
final RadioButton femaleRadioButton = (RadioButton) this
.findViewById(R.id.female);
//radioGroup是把里面所有的button都设置id,通过判断id,来获得所选性别。
radioGroup = (RadioGroup) this.findViewById(R.id.radiogroup);
//注意的是,在建立监听器时,必须new RadioGroup.OnCheckChangeListener(),自动生成的代码不会有 RadioGroup.;
radioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//判断选中的id。
if (checkedId == manRadioButton.getId()) {
Toast.makeText(getApplicationContext(), "男", 1)
.show();
} else if (checkedId == femaleRadioButton.getId()) {
Toast.makeText(getApplicationContext(), "女", 1)
.show();
}
}
});
方法二:
/**
* 这个方法,不用给每个子radioButton设置id,对一个普通的button按钮设置监听事件,
* 通过radioGroup得getChildCount()方法获得子radioButton的个数, 利用一个for循环,获得每一个
*/
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int len = radioGroup.getChildCount();
for (int i = 0; i < len; i++) {
RadioButton radioButton = (RadioButton) radioGroup
.getChildAt(i);
if (radioButton.isChecked()) {
String msg = "方法二:";
msg += radioButton.getText().toString().trim();
Toast.makeText(getApplicationContext(), msg, 1).show();
}
}
}
});