如何一个监听附加到一个无线电按钮 [英] how to attach a listener to a radio button
本文介绍了如何一个监听附加到一个无线电按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个显示不同的单选按钮的活动。的单选按钮被分组在radiogroups。我想一些单选按钮的某个单选按钮被选中后消失。例如,当事件按钮被选中了秋天,行程和疾病单选按钮消失。我怎样才能做到这一点?
我有foolowing code,但需要以某种方式附加监听器事件按钮。
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.reportsomethinglayout);
分辨率=(EditText上)findViewById(R.id.editTextresolution);
// resolution.setInputType(InputType.TYPE_NULL);
// showSoftKeyboard(分辨率);
事故=(单选)findViewById(R.id.radioButtonaccident);
事故=(单选)findViewById(R.id.radioButtonincident);
关注=(单选)findViewById(R.id.radioButtonconcern);
下降=(单选)findViewById(R.id.radioButtonfall);
行程=(单选)findViewById(R.id.radioButtonTrip);
疾病=(单选)findViewById(R.id.radioButtonillness);
}
公共无效onRadioButtonClicked(查看视图){
//是按钮现在检查?
布尔查=((单选)查看).isChecked();
//检查哪个单选按钮被点击
开关(view.getId()){
案例R.id.radioButtonaccident:
如果(检查)
Log.e(TAG,意外单选按钮选中);
打破;
案例R.id.radioButtonincident:
如果(检查)
Log.e(TAG,事件单选按钮选中);
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
打破;
}
}
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:背景=@可绘制/ carefreebgscaledalphajpg>
<滚动型
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:ID =@ + ID / linearlayoutasscrollneedsonenamedchild>
<的TextView
机器人:ID =@ + ID / reportsomethingtitletextview
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=护老者报告
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:layout_gravity =中心/>
<的TextView
机器人:ID =@ + ID / textViewcategory
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=目录/>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< RadioGroup中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选按钮
机器人:ID =@ + ID / radioButtonaccident
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=意外/>
<单选按钮
机器人:ID =@ + ID / radioButtonincident
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=事件/>
<单选按钮
机器人:ID =@ + ID / radioButtonconcern
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=关注/>
< / RadioGroup中>
< / LinearLayout中>
<的TextView
机器人:ID =@ + ID / textViewspacer
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=/>
<的TextView
机器人:ID =@ + ID / textViewtype
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=输入/>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< RadioGroup中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选按钮
机器人:ID =@ + ID / radioButtonfall
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=秋天/>
<单选按钮
机器人:ID =@ + ID / radioButtonTrip
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=跳闸/>
<单选按钮
机器人:ID =@ + ID / radioButtonillness
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=疾病/>
< / RadioGroup中>
< / LinearLayout中>
<的TextView
机器人:ID =@ + ID / textViewspacer2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=/>
<的TextView
机器人:ID =@ + ID / textViewaction
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=操作/>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< RadioGroup中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选按钮
机器人:ID =@ + ID / radioButtonCallDoctor
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=呼叫医生/>
<单选按钮
机器人:ID =@ + ID / radioButtoncalledkin
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=叫近亲/>
< / RadioGroup中>
< / LinearLayout中>
<的TextView
机器人:ID =@ + ID / textViewspacer3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=/>
<的TextView
机器人:ID =@ + ID / textViewresolution
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=分辨率/>
<的EditText
机器人:ID =@ + ID / editTextresolution
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:EMS =1
机器人:行=8
机器人:inputType =textMultiLine
>
<不是requestFocus />
< /的EditText>
<按钮
机器人:ID =@ + ID / buttonsubmit
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:文本=提交/>
< / LinearLayout中>
< /滚动型>
< / LinearLayout中>
解决方案
试试这样: -
RadioGroup中组=(RadioGroup中)findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId)
{
// TODO自动生成方法存根
如果(radiobutton1.isChecked())
{
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
}
否则,如果(radiobutton2.isChecked())
{
}
}
});
I have an activity that displays various radiobuttons. The radiobuttons are grouped in radiogroups. I want some of the radiobutton to disappear when a certain radiobutton is checked. eg when the incident button is checked the fall, trip and illness radiobutton disappear. how can i achieve this?
I have the foolowing code but need to somehow attach a listener to the incident button.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reportsomethinglayout);
resolution = (EditText)findViewById(R.id.editTextresolution);
// resolution.setInputType(InputType.TYPE_NULL);
// showSoftKeyboard(resolution);
accident = (RadioButton)findViewById(R.id.radioButtonaccident);
incident = (RadioButton)findViewById(R.id.radioButtonincident);
concern = (RadioButton)findViewById(R.id.radioButtonconcern);
fall = (RadioButton)findViewById(R.id.radioButtonfall);
trip = (RadioButton)findViewById(R.id.radioButtonTrip);
illness = (RadioButton)findViewById(R.id.radioButtonillness);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioButtonaccident:
if (checked)
Log.e(TAG, "accident radiobutton checked");
break;
case R.id.radioButtonincident:
if (checked)
Log.e(TAG, "incident radiobutton checked");
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
break;
}
}
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/carefreebgscaledalphajpg" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearlayoutasscrollneedsonenamedchild" >
<TextView
android:id="@+id/reportsomethingtitletextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carer Reporting"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center" />
<TextView
android:id="@+id/textViewcategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Category" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonaccident"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accident" />
<RadioButton
android:id="@+id/radioButtonincident"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Incident" />
<RadioButton
android:id="@+id/radioButtonconcern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Concern" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewtype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonfall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fall" />
<RadioButton
android:id="@+id/radioButtonTrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trip" />
<RadioButton
android:id="@+id/radioButtonillness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Illness" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Action" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonCallDoctor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Doctor" />
<RadioButton
android:id="@+id/radioButtoncalledkin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Called next of kin" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewresolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Resolution" />
<EditText
android:id="@+id/editTextresolution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="1"
android:lines="8"
android:inputType="textMultiLine"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Submit" />
</LinearLayout>
</ScrollView>
</LinearLayout>
解决方案
try like this:-
RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(radiobutton1.isChecked())
{
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
}
else if(radiobutton2.isChecked())
{
}
}
});
这篇关于如何一个监听附加到一个无线电按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文