how to attach a listener to a radio button(如何将侦听器附加到单选按钮)
问题描述
我有一个显示各种单选按钮的活动.单选按钮分组在单选组中.当检查某个radiobutton时,我想要一些radiobutton消失.例如,当事件按钮被选中时,跌倒、旅行和疾病单选按钮消失.我怎样才能做到这一点?
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>
推荐答案
试试这样:-
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()) {
}
}
});
这篇关于如何将侦听器附加到单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将侦听器附加到单选按钮
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01