您的位置:首页 > 产品设计 > UI/UE

android基本 UI(三)—单选RadioButton与复选按钮CheckBox

2014-12-08 14:54 323 查看
源码下载:点击打开链接






(一)单选按钮RadioButton

单选按钮RadioButton,是仅可以选择一个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。




单选按钮要声明在RadioGroup中,RadioGroup是RadioButton的承载体,程序运行时不可见,应用程序中可能包含一个或多个RadioGroup,RadioGroup是线性布局LinearLayout的子类。

单选按钮状态更改的监听,是要给它的RadioGroup添加setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。注意监听器类型和复选按钮CheckBox是不相同的。

单选按钮的通常用法:

1、用xml描述的RadioGroup和RadioButton应用的界面设计

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<RadioGroup android:id="@+id/radioGroup"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<RadioButton android:id="@+id/java"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="java" />

<RadioButton android:id="@+id/dotNet"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="dotNet" />

<RadioButton android:id="@+id/php"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="PHP" />

</RadioGroup>

</LinearLayout>

2、引用处理程序

public void onCreate(Bundle savedInstanceState) {

......

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton radioButton = (RadioButton) findViewById(checkedId);

Log.i(TAG, String.valueOf(radioButton.getText()));

}

});

}

RadioButton 和RadioGroup常用的方法及说明:

如下代码:

RadioGroup.check(R.id.dotNet);//将id名为dotNet的单选框设置成选中状态。

(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。

RadioButton.getText( );//获取单选框的值

(二)复选按钮CheckBox

复选按钮CheckBox,是一个同时可以选择多个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。





复选按钮CheckBox的通常用法:

1、用xml描述的CheckBox应用界面设计

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="fill_parent">

<CheckBox android:id="@+id/checkboxjava"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="java" />

<CheckBox android:id="@+id/checkboxdotNet"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="dotNet" />

<CheckBox android:id="@+id/checkboxphp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="PHP" />



<Button android:id="@+id/checkboxButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="获取值" />

</LinearLayout>

2、 引用xml描述的代码处理:

public class CheckBoxActivity extends Activity {

private static final String TAG = "CheckBoxActivity";

private List<CheckBox> checkboxs = new ArrayList<CheckBox>();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.checkbox);

checkboxs.add((CheckBox) findViewById(R.id.checkboxdotNet));

checkboxs.add((CheckBox) findViewById(R.id.checkboxjava));

checkboxs.add((CheckBox) findViewById(R.id.checkboxphp));

checkboxs.get(1).setChecked(true);//设置成选中状态

for(CheckBox box : checkboxs){

box.setOnCheckedChangeListener(listener);

}

Button button = (Button)findViewById(R.id.checkboxButton);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

List<String> values = new ArrayList<String>();

for(CheckBox box : checkboxs){

if(box.isChecked()){

values.add(box.getText().toString());

}

}

Toast.makeText(CheckBoxActivity.this, values.toString(), 1).show();

}

});

}

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

CheckBox checkBox = (CheckBox) buttonView;

Log.i(TAG, "isChecked="+ isChecked +",value="+ checkBox.getText());//输出单选框的值

}

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: