Android UI控件三

来源:互联网 发布:99云呼软件 编辑:程序博客网 时间:2024/06/05 00:36
Android UI控件三

一、RadioButton选项按钮控件

选项按钮可以用于多选一的应用中,如果想在选中的某一个选项按钮后,其它的选项按钮都被设置为未选中的状态,需要将<RadioButton>添加到<RadioGroup>标签中。

1.单选按钮的使用

public class MainActivity extends Activity {private RadioGroup group;private Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);group = (RadioGroup) findViewById(R.id.radiogroup01);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {int len = group.getChildCount();String msg = "";for (int i = 0; i < len; i++) {RadioButton radioButton = (RadioButton) group.getChildAt(i);if (radioButton.isChecked()) {msg = radioButton.getText().toString();break;}}Toast.makeText(MainActivity.this, msg, 1).show();}});}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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/radiogroup01"        android:layout_width="match_parent"        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/button01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="选择性别" /></LinearLayout>

二、ToggleButton开关状态按钮控件

1.ToggleButton按钮的使用

public class MainActivity extends Activity {private ToggleButton toggleButton;private LinearLayout linearLayout;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);toggleButton = (ToggleButton) findViewById(R.id.ToggleButton);linearLayout = (LinearLayout) findViewById(R.id.linearlayout01);toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (toggleButton.isChecked()) {linearLayout.setOrientation(1);} else {linearLayout.setOrientation(0);}}});}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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:id="@+id/linearlayout01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        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" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="button3" />    </LinearLayout></LinearLayout>

三、CheckBox复选框控件

CheckBox默认的情况下是未选中的状态,如果想修改这个默认值的话,可以将<checkbox>的android:checked设置为true或者使用CheckBox.setChecked方法设置都可以实现复选的功能。

1.复选框控件的使用

public class MainActivity extends Activity implements OnClickListener {private List<CheckBox> CheckBoxs = new ArrayList<CheckBox>();private Button btn;// private CheckBox checkBox;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// setContentView(R.layout.main);String[] CheckBoxText = new String[] { "地球", "亚洲", "中国", "上海", "杨浦","浦东" };// 动态加载布局LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.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]);linearLayout.addView(checkBox, i);}setContentView(linearLayout);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(this);}public void onClick(View v) {String msg = "";for (CheckBox checkBox : CheckBoxs) {if (checkBox.isChecked()) {msg += checkBox.getText().toString() + "\n";}}if (msg.equals("")) {msg = "您还没有选择!";}new AlertDialog.Builder(this).setMessage(msg).setPositiveButton("关闭", null).show();}}

checkbox.xml

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

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="确定" /></LinearLayout>





原创粉丝点击