如何将侦听器附加到单选按钮
Posted
技术标签:
【中文标题】如何将侦听器附加到单选按钮【英文标题】:how to attach a listener to a radio button 【发布时间】:2012-11-11 20:02:29 【问题描述】:我有一个显示各种单选按钮的活动。单选按钮分组在单选组中。我希望某些单选按钮在选中某个单选按钮时消失。例如,当事件按钮被选中时,跌倒、旅行和疾病单选按钮消失。我怎样才能做到这一点?
我有愚蠢的代码,但需要以某种方式将侦听器附加到事件按钮。
@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_
android:layout_
android:orientation="vertical"
android:background="@drawable/carefreebgscaledalphajpg" >
<ScrollView
android:layout_
android:layout_
>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:id="@+id/linearlayoutasscrollneedsonenamedchild" >
<TextView
android:id="@+id/reportsomethingtitletextview"
android:layout_
android:layout_
android:text="Carer Reporting"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center" />
<TextView
android:id="@+id/textViewcategory"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Category" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonaccident"
android:layout_
android:layout_
android:text="Accident" />
<RadioButton
android:id="@+id/radioButtonincident"
android:layout_
android:layout_
android:text="Incident" />
<RadioButton
android:id="@+id/radioButtonconcern"
android:layout_
android:layout_
android:text="Concern" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewtype"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonfall"
android:layout_
android:layout_
android:text="Fall" />
<RadioButton
android:id="@+id/radioButtonTrip"
android:layout_
android:layout_
android:text="Trip" />
<RadioButton
android:id="@+id/radioButtonillness"
android:layout_
android:layout_
android:text="Illness" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer2"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewaction"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Action" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonCallDoctor"
android:layout_
android:layout_
android:text="Call Doctor" />
<RadioButton
android:id="@+id/radioButtoncalledkin"
android:layout_
android:layout_
android:text="Called next of kin" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/textViewspacer3"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="" />
<TextView
android:id="@+id/textViewresolution"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Resolution" />
<EditText
android:id="@+id/editTextresolution"
android:layout_
android:layout_
android:ems="1"
android:lines="8"
android:inputType="textMultiLine"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonsubmit"
android:layout_
android:layout_
android:layout_gravity="center"
android:text="Submit" />
</LinearLayout>
</ScrollView>
</LinearLayout>
【问题讨论】:
【参考方案1】:试试这样:-
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
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())
);
【讨论】:
【参考方案2】:您可以使用setOnCheckedChangeListener
在RadioGroup
上设置侦听器。 onCheckedChanged
回调在checkedId
参数中接收新选中按钮的ID。
在您的情况下,只需向您的广播组添加一个 ID(以便从您的代码中检索它)
<RadioGroup
android:id="@+id/category_group"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal" >
并使用以下代码:
RadioGroup categoryGroup = (RadioGroup) findViewById(R.id.category_group);
categoryGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
switch(checkedId)
case R.id.radioButtonincident:
// 'Incident' checked
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
break;
case R.id.radioButtonaccident:
// 'Accident' checked
break;
case R.id.radioButtonconcern:
// 'Concern' checked
break;
);
【讨论】:
以上是关于如何将侦听器附加到单选按钮的主要内容,如果未能解决你的问题,请参考以下文章
如何使用jQuery将check attr添加到单选按钮nth-child?