android radiobutton 分组,Android 第十课——UI RadioButton
常用基礎(chǔ)空組件
5 RadioGroup RadioButton
單選按鈕時(shí)常用組件之一,但是開(kāi)發(fā)過(guò)程中雖然很多地方會(huì)用到單選按鈕,但是卻不會(huì)用Android系統(tǒng)提供的原始樣式,類似我們?cè)趯慼tml時(shí),不同的瀏覽器提供的單選按鈕是不一樣的,兼容性也很差,一般用圖片替代偽裝。但是無(wú)論如何偽裝樣式,單選按鈕的功能都是一樣的。下面就用一個(gè) 加減乘除 運(yùn)算器來(lái)講解單選按鈕的功能,至于樣式以后提供。
layout中組件:
android:id="@+id/operation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:id="@+id/addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/addition"/>
android:id="@+id/subtraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtraction"/>
Activity中組件:
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//?獲取RadioGroup
radioGroup?=?(RadioGroup)this.findViewById(R.id.operation);
//?為RadioGroup注冊(cè)事件監(jiān)聽(tīng)(當(dāng)單選按鈕被選中時(shí)這個(gè)類的onCheckedChanged方法被觸發(fā))
radioGroup.setOnCheckedChangeListener(new?MainActivity.RadioGroupDefineImpl());
}
//?Activity內(nèi)部類,實(shí)現(xiàn)OnCheckedChangeListener接口同時(shí)實(shí)現(xiàn)onCheckedChanged方法
class?RadioGroupDefineImpl?implements?OnCheckedChangeListener{
@Override
public?void?onCheckedChanged(RadioGroup?group,?int?id)?{
//?TODO?Auto-generated?method?stub
//?group就是被選中那個(gè)Radio所屬的組RadioGroup對(duì)象,id則是被選中那個(gè)Radio的id,所以通過(guò)如下方式可以獲取RadioButton,也可以通過(guò)radioGroup.getCheckedRadioButtonId()獲取id
RadioButton?radio?=?(RadioButton)MainActivity.this.findViewById(id);
Toast.makeText(MainActivity.this,?radio.getText(),?Toast.LENGTH_SHORT).show();
}
}
注意:
1)單選按鈕RadioButton不能單獨(dú)存在,他必須有明確的分組RadioGroup,即每一個(gè)RadioButton都應(yīng)該是RadioGroup的子組件。
2)給每一個(gè)RadioGroup和RadioButton注冊(cè)一個(gè)ID,便于java程序中獲取
3)checked="true" 表示按鈕默認(rèn)被選中,使用單選按鈕時(shí)最好有一個(gè)默認(rèn)選中的選項(xiàng),否則你干嘛不使用多選按鈕呢?
4)在java程序Activity的onCreate方法中我們只需獲取RadioGroup,然后為RadioGroup注冊(cè)事件監(jiān)聽(tīng),這個(gè)事件監(jiān)聽(tīng)是一個(gè)?OnCheckedChangeListener 的實(shí)例對(duì)象,同時(shí)我們要實(shí)現(xiàn)onCheckedChanged方法。在監(jiān)聽(tīng)器中我們可以通過(guò)參數(shù)獲取哪個(gè)RadioButton被選中。這是與Button或CheckBox最大的區(qū)別,他們都是對(duì)單獨(dú)的按鈕注冊(cè)事件監(jiān)聽(tīng)。也可以通過(guò)radioGroup.getCheckedRadioButtonId()獲取被選中的RadioButton的id。
5)當(dāng)我們?cè)趈ava程序中具體判斷按個(gè)單選按鈕被選中時(shí),一般使用ID如下判斷:
int?operation?=?radioGroup.getCheckedRadioButtonId()
switch(operation){
case?R.id.subtraction:
//?coding
break;
}
下面是一個(gè)使用RadioButton做的加減乘除運(yùn)算器:
layout.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:id="@+id/param1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/defalut0"
android:inputType="number"
/>
android:id="@+id/operation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:id="@+id/addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/addition"/>
android:id="@+id/subtraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtraction"/>
android:id="@+id/multiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multiplication"/>
android:id="@+id/division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/division"/>
android:id="@+id/param2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="@string/defalut0"/>
android:id="@+id/mainActivity_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mainActivity_result"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/defalut0"/>
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,?"除數(shù)不能為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);
}
}
圖片:
總結(jié)
以上是生活随笔為你收集整理的android radiobutton 分组,Android 第十课——UI RadioButton的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 个性幽默签名
- 下一篇: android file mkdir,a