【问题标题】:Radio Button findViewById within a Fragment片段中的单选按钮 findViewById
【发布时间】:2016-12-19 00:24:15
【问题描述】:

我环顾四周并实现了一些我发现的东西,以便让 findViewById 在我的 Fragment 中工作。但是,我得到了无法解析方法 findViewById 或“无法访问的语句”。我不完全确定我哪里出错了。

这是我的 Fragment 的代码。

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

/**
 * Created by James Singleton on 8/8/2016.
 */

public class SettingsFragment extends Fragment
{
    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.settings_layout, container, false);
        return myView;

        RadioGroup radioGroup = (RadioGroup) getView().findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected

                switch(checkedId) {
                    case R.id.radioButton7:
                        // switch to fragment 1
                        break;
                    case R.id.radioButton6:
                        // Fragment 2
                        break;
                }
            }
        });
    }
}

这是我的布局代码

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:text="Use Cell Data to retrieve driving data:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textSize="18sp"
    android:textColor="@color/cast_expanded_controller_background_color"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<RadioGroup
    android:layout_width="89dp"
    android:layout_height="69dp"
    android:weightSum="1"
    android:layout_below="@+id/textView"
    android:id="@+id/radioGroup">

    <RadioButton
        android:text="Enable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton7"
        android:layout_weight="1" />

    <RadioButton
        android:text="Disable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton6"
        android:layout_weight="1"
        android:checked="true" />
</RadioGroup>

【问题讨论】:

  • findViewById in Fragment的可能重复
  • 你在 return 语句之后写的任何东西都不起作用......这就是你收到 Unreachable Statement 警告的原因......所以将所有其他操作移到上面 return myView;
  • @SabeerMohammed,谢谢 :)

标签: android android-fragments radio-button radio-group android-radiogroup


【解决方案1】:

像这样更改您的代码:

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.settings_layout, container, false);


        RadioGroup radioGroup = (RadioGroup) myView .findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected

                switch(checkedId) {
                    case R.id.radioButton7:
                        // switch to fragment 1
                        break;
                    case R.id.radioButton6:
                        // Fragment 2
                        break;
                }
            }
        });

        return myView;
    }

【讨论】:

  • 这个有效。当 7 分钟结束时将接受!谢谢!
  • 是的,我尝试做一件事,在启动应用程序时我会弹出一个询问用户是否要使用 WiFi 或移动数据以及不再显示的选项。但是,如果用户想改变他们的决定怎么办?我实现了一个带有无线电组的设置按钮。现在我只需要弄清楚如何保存更改...
  • 获取单选按钮的值并将其保存在 sharedpreferences 中。并在您的设置活动中使用此值。有关 sharedpreferences 的详细信息,请查看此链接--developer.android.com/training/basics/data-storage/…
  • 我可以在 onClick 中做到这一点吗?此外,我会在上面的代码中在哪里添加 onClick ?既然做了之后返回myView;不会产生任何结果。
  • return myView 应该是片段类中的最后一条语句。参见@Sabeer Mohammed 的评论
【解决方案2】:

在返回 onCreateView() 中的视图之前,请使用您膨胀的视图:

view.findViewById(R.id.your_id);

所以在你的情况下,它会像:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.settings_layout, container, false);

    RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radioGroup);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected

            switch(checkedId) {
                case R.id.radioButton7:
                    // switch to fragment 1
                    break;
                case R.id.radioButton6:
                    // Fragment 2
                    break;
            }
        }
    });
    return myView;
}

【讨论】:

    【解决方案3】:
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.settings_layout, container, false);
        RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        return myView;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-02
      • 2020-04-11
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多