首页 > 代码库 > Android 第十课——UI RadioButton
Android 第十课——UI RadioButton
常用基础空组件
5 RadioGroup RadioButton
单选按钮时常用组件之一,但是开发过程中虽然很多地方会用到单选按钮,但是却不会用Android系统提供的原始样式,类似我们在写html时,不同的浏览器提供的单选按钮是不一样的,兼容性也很差,一般用图片替代伪装。但是无论如何伪装样式,单选按钮的功能都是一样的。下面就用一个 加减乘除 运算器来讲解单选按钮的功能,至于样式以后提供。
layout中组件:
<RadioGroup android:id="@+id/operation" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/addition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/addition"/> <RadioButton android:id="@+id/subtraction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/subtraction"/> </RadioGroup>
Activity中组件:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取RadioGroup radioGroup = (RadioGroup)this.findViewById(R.id.operation); // 为RadioGroup注册事件监听(当单选按钮被选中时这个类的onCheckedChanged方法被触发) radioGroup.setOnCheckedChangeListener(new MainActivity.RadioGroupDefineImpl()); } // Activity内部类,实现OnCheckedChangeListener接口同时实现onCheckedChanged方法 class RadioGroupDefineImpl implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup group, int id) { // TODO Auto-generated method stub // group就是被选中那个Radio所属的组RadioGroup对象,id则是被选中那个Radio的id,所以通过如下方式可以获取RadioButton,也可以通过radioGroup.getCheckedRadioButtonId()获取id RadioButton radio = (RadioButton)MainActivity.this.findViewById(id); Toast.makeText(MainActivity.this, radio.getText(), Toast.LENGTH_SHORT).show(); } }
注意:
1)单选按钮RadioButton不能单独存在,他必须有明确的分组RadioGroup,即每一个RadioButton都应该是RadioGroup的子组件。
2)给每一个RadioGroup和RadioButton注册一个ID,便于java程序中获取
3)checked="true" 表示按钮默认被选中,使用单选按钮时最好有一个默认选中的选项,否则你干嘛不使用多选按钮呢?
4)在java程序Activity的onCreate方法中我们只需获取RadioGroup,然后为RadioGroup注册事件监听,这个事件监听是一个 OnCheckedChangeListener 的实例对象,同时我们要实现onCheckedChanged方法。在监听器中我们可以通过参数获取哪个RadioButton被选中。这是与Button或CheckBox最大的区别,他们都是对单独的按钮注册事件监听。也可以通过radioGroup.getCheckedRadioButtonId()获取被选中的RadioButton的id。
5)当我们在java程序中具体判断按个单选按钮被选中时,一般使用ID如下判断:
int operation = radioGroup.getCheckedRadioButtonId() switch(operation){ case R.id.subtraction: // coding break; }
下面是一个使用RadioButton做的加减乘除运算器:
layout.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" > <EditText android:id="@+id/param1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/defalut0" android:inputType="number" /> <RadioGroup android:id="@+id/operation" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/addition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/addition"/> <RadioButton android:id="@+id/subtraction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/subtraction"/> <RadioButton android:id="@+id/multiplication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/multiplication"/> <RadioButton android:id="@+id/division" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/division"/> </RadioGroup> <EditText android:id="@+id/param2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:text="@string/defalut0"/> <Button android:id="@+id/mainActivity_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/mainActivity_result" /> <TextView android:id="@+id/result" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/defalut0"/> </LinearLayout>
java Activity:
package com.hnust.moon; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private static final String TAG = MainActivity.class.getSimpleName(); private RadioGroup radioGroup; private Button btn; private EditText param1; private EditText param2; private TextView rsv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); param1 = (EditText)MainActivity.this.findViewById(R.id.param1); param2 = (EditText)MainActivity.this.findViewById(R.id.param2); rsv = (TextView)MainActivity.this.findViewById(R.id.result); btn = (Button)this.findViewById(R.id.mainActivity_btn); btn.setOnClickListener(new MainActivity.ButtonDefineImpl()); radioGroup = (RadioGroup)this.findViewById(R.id.operation); radioGroup.setOnCheckedChangeListener(new MainActivity.RadioGroupDefineImpl()); } class RadioGroupDefineImpl implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup group, int id) { // TODO Auto-generated method stub RadioButton radio = (RadioButton)MainActivity.this.findViewById(id); Toast.makeText(MainActivity.this, radio.getText(), Toast.LENGTH_SHORT).show(); } } /** * * @author heweipo * */ class ButtonDefineImpl implements OnClickListener{ @Override public void onClick(View view) { // TODO Auto-generated method stub int operation = radioGroup.getCheckedRadioButtonId(); rsv.setText(calcurateResult(operation, Integer.parseInt(param1.getText().toString()), Integer.parseInt(param2.getText().toString()))+""); } } private int calcurateResult(int operation , int param1 , int param2){ int result = 0; switch(operation){ case R.id.subtraction: result = param1 - param2; break; case R.id.multiplication: result = param1 * param2; break; case R.id.division: if(param2 == 0){Toast.makeText(this, "除数不能为0", Toast.LENGTH_SHORT).show();break;} result = param1 / param2; break; default: result = param1 + param2; break; } return result; } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 8080, 1, R.string.exit); getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_settings: break; case R.id.menu_addition: radioGroup.check(R.id.addition); rsv.setText(calcurateResult(R.id.addition, Integer.parseInt(param1.getText().toString()), Integer.parseInt(param2.getText().toString()))+""); break; case R.id.menu_subtraction: radioGroup.check(R.id.subtraction); rsv.setText(calcurateResult(R.id.subtraction, Integer.parseInt(param1.getText().toString()), Integer.parseInt(param2.getText().toString()))+""); break; case R.id.menu_multiplication: radioGroup.check(R.id.multiplication); rsv.setText(calcurateResult(R.id.multiplication, Integer.parseInt(param1.getText().toString()), Integer.parseInt(param2.getText().toString()))+""); break; case R.id.menu_division: radioGroup.check(R.id.division); rsv.setText(calcurateResult(R.id.division, Integer.parseInt(param1.getText().toString()), Integer.parseInt(param2.getText().toString()))+""); break; case 8080: finish(); break; } return super.onOptionsItemSelected(item); } }
图片:
Android 第十课——UI RadioButton