【问题标题】:Listeners do not work in my Fragment听众在我的片段中不起作用
【发布时间】:2018-06-03 23:29:55
【问题描述】:

当我尝试将 Button 的 onClickListener 和 RadioGroup 的 onCheckedChangeListener 实现到我的 Fragment 时遇到问题。这些监听器都没有在我的 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()-Method 实现到不同的 Fragment 中,并且 Button 工作正常...

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

【问题讨论】:

  • 在您的 onCreateView 中而不是 return inflater.inflate(R.layout.content_general, container, false); 中尝试仅放入 return view;

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


【解决方案1】:

而不是

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

【讨论】:

  • 非常感谢!它真的对我有用。你介意解释一下为什么这是必要的吗?我理解它的方式,我之前也给出了这个确切的观点,不是吗?是不是因为视图正在更改,并且应该返回更改后的值?
  • 不客气,片段被膨胀了两次,您需要膨胀一次并在片段内调用 xml 视图。您正在返回第二个 xml,这就是它导致问题的原因。
猜你喜欢
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
相关资源
最近更新 更多