听众不能在我的片段中工作

时间:2018-06-03 15:02:52

标签: android android-fragments button radio-button listener

当我尝试将Button的onClickListener和RadioGroup的onCheckedChangeListener实现到我的Fragment中时遇到了问题。这些监听器都没有在我的片段中工作,onClick() - 和onCheckedChange() - 方法都没有被调用,即使它们应该被调用。

这是我的片段:

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() - 方法实现到一个不同的Fragment中,Button工作正常...

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

1 个答案:

答案 0 :(得分:1)

而不是

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

更改为

return view;

完成 onCreateView 代码在这里,我在radiobutton上点击了toast

@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;
}