經常使用基礎空組件html
5 RadioGroup RadioButtonjava
單選按鈕時經常使用組件之一,可是開發過程當中雖然不少地方會用到單選按鈕,可是卻不會用Android系統提供的原始樣式,相似咱們在寫html時,不一樣的瀏覽器提供的單選按鈕是不同的,兼容性也不好,通常用圖片替代假裝。可是不管如何假裝樣式,單選按鈕的功能都是同樣的。下面就用一個 加減乘除 運算器來說解單選按鈕的功能,至於樣式之後提供。android
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中組件:
app
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(); } }
注意:ide
1)單選按鈕RadioButton不能單獨存在,他必須有明確的分組RadioGroup,即每個RadioButton都應該是RadioGroup的子組件。this
2)給每個RadioGroup和RadioButton註冊一個ID,便於java程序中獲取spa
3)checked="true" 表示按鈕默認被選中,使用單選按鈕時最好有一個默認選中的選項,不然你幹嗎不使用多選按鈕呢?code
4)在java程序Activity的onCreate方法中咱們只需獲取RadioGroup,而後爲RadioGroup註冊事件監聽,這個事件監聽是一個 OnCheckedChangeListener 的實例對象,同時咱們要實現onCheckedChanged方法。在監聽器中咱們能夠經過參數獲取哪一個RadioButton被選中。這是與Button或CheckBox最大的區別,他們都是對單獨的按鈕註冊事件監聽。也能夠經過radioGroup.getCheckedRadioButtonId()獲取被選中的RadioButton的id。xml
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); } }
圖片: