侦听器在我的 Fragment 中不起作用

P.斯蒂芬尼

当我尝试在 Fragment 中实现 Button 的 onClickListener 和 RadioGroup 的 onCheckedChangeListener 时遇到问题。这些侦听器都没有在我的 Fragment 中工作,即使应该调用 onClick()- 和 onCheckedChange()-Method 也没有被调用。

这是我的片段:

public class GeneralFragment extends Fragment {

public static Class startClass;
RadioGroup radioGroup;
RadioButton r1, r2, r3;
String start;
Button confirm;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.content_general, container, false);
    //View view = (View)getView().inflate(getContext(), R.layout.content_general, null);

    //start = "FirstFragment";

    radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            System.out.println(checkedId);
            switch (checkedId) {
                case R.id.radioButton1:
                    startClass = FirstFragment.class;
                    start = "FirstFragment";
                    break;
                case R.id.radioButton2:
                    startClass = SecondFragment.class;
                    start = "SecondFragment";
                    break;
                case R.id.radioButton3:
                    startClass = ThirdFragment.class;
                    start = "ThirdFragment";
                    break;
                default:
                    startClass = FirstFragment.class;
                    start = "FirstFragment";

            }
        }
    });                


    confirm = (Button) view.findViewById(R.id.confirmButton);

    confirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            writeToFile(start, getContext());
            System.out.println("Succesfully written!");
        }
    });        

    return inflater.inflate(R.layout.content_general, container, false);
}

private void writeToFile(String data, Context context) {
    System.out.println("writing...");
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("settings.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

}

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">        

<TextView
    android:id="@+id/textView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/setting_two_title" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/setting_two_first" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/setting_two_second" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/setting_two_third" />
</RadioGroup>

<Button
    android:id="@+id/confirmButton"
    android:layout_width="147dp"
    android:layout_height="wrap_content"
    android:text="@string/setting_three" />

我找不到未注册单选按钮的单击/更改的原因。我以这种方式将 onClick()-Method 实现到不同的 Fragment 中,并且 Button 工作正常......

我真的不知道这个问题可能是什么,我感谢你们给我的每一个提示。

阿米尔朵拉。

代替

return inflater.inflate(R.layout.content_general, container, false);

改成

return view;

完整的onCreateView代码在这里,我在单击单选按钮时显示吐司

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.content_general, container, false);
    //View view = (View)getView().inflate(getContext(), R.layout.content_general, null);

    //start = "FirstFragment";

    radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            System.out.println(checkedId);
            switch (checkedId) {
                case R.id.radioButton1:
                    Toast.makeText(getContext(), "clicked one", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.radioButton2:
                    Toast.makeText(getContext(), "clicked two", Toast.LENGTH_SHORT).show();

                    break;
                case R.id.radioButton3:
                    Toast.makeText(getContext(), "clicked three", Toast.LENGTH_SHORT).show();

                    break;
                default:


            }
        }
    });


    confirm = (Button) view.findViewById(R.id.confirmButton);

    confirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            writeToFile(start, getContext());
            System.out.println("Succesfully written!");
        }
    });

    return view;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章