处理片段中的单选组和按钮
Handling Radio Group and Button in Fragment
首先看一下我的片段xml文件的一部分`
<LinearLayout
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rbclick"
android:text="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="4" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="5" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="rbclick"
android:text="6" />
</RadioGroup>
</LinearLayout>
现在看看我的片段javaclass
public class MealEntry extends Fragment {
private MealEntryViewModel mViewModel;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "MealEntry";
public RadioGroup rg;
RadioButton rb;
public static MealEntry newInstance() {
return new MealEntry();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
return view;
}
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(radiobtnid);
}
在此过程之前,我的意思是(rbclick 函数)可以处理 Activity 中的单选组和单选按钮(我重复 Activity)。但在 Fragment 中,onClick 方法不起作用。我该如何解决这个问题?我试过这种方式的片段。
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)getActivity().findViewById(radiobtnid);
}
我认为这个处理程序不工作。因为当我点击单选按钮时它崩溃了。
这是日志猫。
12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.closeup, PID: 3720
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9
public void rbclick(View v)
中的参数 View v
是您要查找的 RadioButton
尝试使用下面的代码:
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)v.findViewById(radiobtnid);
}
这是在片段中处理无线电组的解决方案。
1.Remove meal_entry_fragment.xml 文件中每个 RadioButton 的所有行 android:onClick="rbclick"
,并为每个 RadioButton 创建一个 ID。
<?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="wrap_content"
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="4" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="5" />
<RadioButton
android:id="@+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="6" />
</RadioGroup>
</LinearLayout>
2.Create MealFragment 中的 OnCheckedChangeListener 实例 class。
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
// Write your code here
break;
case R.id.radio1:
// Write your code here
break;
case R.id.radio2:
// Write your code here
break;
case R.id.radio3:
// Write your code here
break;
case R.id.radio4:
// Write your code here
break;
case R.id.radio5:
// Write your code here
break;
case R.id.radio6:
// Write your code here
break;
default:
break;
}
}
};
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
return view;
}
首先看一下我的片段xml文件的一部分`
<LinearLayout
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rbclick"
android:text="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="4" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="5" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="rbclick"
android:text="6" />
</RadioGroup>
</LinearLayout>
现在看看我的片段javaclass
public class MealEntry extends Fragment {
private MealEntryViewModel mViewModel;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "MealEntry";
public RadioGroup rg;
RadioButton rb;
public static MealEntry newInstance() {
return new MealEntry();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
return view;
}
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(radiobtnid);
}
在此过程之前,我的意思是(rbclick 函数)可以处理 Activity 中的单选组和单选按钮(我重复 Activity)。但在 Fragment 中,onClick 方法不起作用。我该如何解决这个问题?我试过这种方式的片段。
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)getActivity().findViewById(radiobtnid);
}
我认为这个处理程序不工作。因为当我点击单选按钮时它崩溃了。
这是日志猫。
12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.closeup, PID: 3720
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9
public void rbclick(View v)
中的参数 View v
是您要查找的 RadioButton
尝试使用下面的代码:
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)v.findViewById(radiobtnid);
}
这是在片段中处理无线电组的解决方案。
1.Remove meal_entry_fragment.xml 文件中每个 RadioButton 的所有行 android:onClick="rbclick"
,并为每个 RadioButton 创建一个 ID。
<?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="wrap_content"
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="4" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="5" />
<RadioButton
android:id="@+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="6" />
</RadioGroup>
</LinearLayout>
2.Create MealFragment 中的 OnCheckedChangeListener 实例 class。
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
// Write your code here
break;
case R.id.radio1:
// Write your code here
break;
case R.id.radio2:
// Write your code here
break;
case R.id.radio3:
// Write your code here
break;
case R.id.radio4:
// Write your code here
break;
case R.id.radio5:
// Write your code here
break;
case R.id.radio6:
// Write your code here
break;
default:
break;
}
}
};
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
return view;
}