【问题标题】:Android - How to properly show a fragment when a radio button is checked by default?Android - 默认情况下选中单选按钮时如何正确显示片段?
【发布时间】:2017-02-07 04:22:57
【问题描述】:

我正在尝试使用片段制作应用程序,基于以下文档:Fragments

据我所知,我需要:

  • 主机活动 [完成]:

这是我的主机活动 (.java)

    public class SphereSurfaceArea extends AppCompatActivity
    {
       RadioGroup rg_sa;
       RadioButton rb_grad, rb_gvol, rb_gcirc;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sphere_surface_area);

        rg_sa = (RadioGroup) findViewById(R.id.rg_sphere_sa);

        rb_grad = (RadioButton) findViewById(R.id.rb_given_rad);
        rb_gvol = (RadioButton) findViewById(R.id.rb_given_vol);
        rb_gcirc = (RadioButton) findViewById(R.id.rb_given_circ);

        rb_grad.setChecked(true); //Here the radio button is checked 

        rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.rb_given_rad:
                        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();

                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                        fragmentTransaction.add(R.id.fragment_container, new GivenRad());

                        fragmentTransaction.commit();

                        break;
                 }
              }
           });
        }
     }

这是我的主机 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sphere_surface_area"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="dev.indie.nayir55.geometrycompanion.SolidShapesOperations.SphereOperations.SphereSurfaceArea">

    <RadioGroup
        android:id="@+id/rg_sphere_sa"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!--This is the button checked by default-->
        <RadioButton
            android:id="@+id/rb_given_rad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_horizontal"
            android:layout_weight="1"
            android:gravity="center_horizontal|center|start"
            android:text="Given radious"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textSize="18sp"
            />

        <RadioButton
            android:id="@+id/rb_given_vol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:layout_weight="1"
            android:gravity="center_horizontal|center|start"
            android:text="Given volume"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textSize="18sp"/>

        <RadioButton
            android:id="@+id/rb_given_circ"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_horizontal"
            android:layout_weight="1"
            android:gravity="center_horizontal|center|start"
            android:text="Given circumference"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textSize="18sp"/>
    </RadioGroup>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rg_sphere_sa">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>
  • 片段 [完成]:

这是我的片段 .java 代码:

public class GivenRad extends Fragment
{
    public GivenRad()
    {
        // Required empty public constructor
    }

    public static GivenRad newInstance()
    {
        return new GivenRad();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_given_rad, container, false);
        return rootView;
    }

}

这是我的片段 XML 布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             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"
                                             tools:context="dev.indie.nayir55.geometrycompanion.Fragments.SolidGeometry.FragSphere.GivenRad">
    <TextView
        android:text="Radious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView9"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"/>
</android.support.constraint.ConstraintLayout>
  • 容器的 ID [DONE]:

在我的主机 XML 布局中,您会找到容器:

<FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rg_sphere_sa">

    </FrameLayout>
  • 一种动态使用片段的方法 [DONE]:

这里我尝试使用单选按钮和开关动态使用片段:

rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.rb_given_rad:
                        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();

                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                        fragmentTransaction.add(R.id.fragment_container, new GivenRad());

                        fragmentTransaction.commit();

                        break;
                }
            }
        });

所以...我的问题是这样的:

使用单选按钮组和单选按钮,我设置了默认选中的单选按钮,当我打开包含单选按钮和片段的主机活动时,单选按钮被选中,但在我再次检查之前它不会显示片段按钮,如果我使用if 语句也是相同的结果我做错了什么?

【问题讨论】:

    标签: android android-fragments radio-button


    【解决方案1】:

    发生的情况是您默认检查收音机,并且仅在收音机被更改或按下时才加载片段。我建议在运行时加载默认片段,以避免必须点击按钮才能显示它。

    【讨论】:

    • 意思是……在宿主activity里面的xml中插入fragment,然后用switch循环其他2个fragment??
    • @NAYIR55 那行不通。如果您在 XML 中静态指定片段,那么您将无法使用 FragmentTransaction 以编程方式更改它。
    【解决方案2】:

    目前,您只是加载片段以响应onCheckedChanged() 中的事件。您还需要在onCreate() 中加载相应的片段。首先,您可能只想将直接加载片段的代码复制并粘贴到onCreate()。最终,您可以通过将片段加载代码放入从onCreate()onCheckedChanged() 调用的方法(即loadFragment())中来减少重复代码。

    【讨论】:

    • 你能告诉我怎么做吗?我的编码技能有点基础,所有代码都是基于 SO 中的教程和答案以及 Android 开发人员文档
    • @NAYIR55 您已经拥有所需的构建块。诀窍是以正确的顺序将它们放在一起。看看你能不能从我在这里写的内容中弄清楚。试一试后,再回来问一些问题。
    • 只是为了确定...... 1)创建一个新方法来加载片段:将代码从案例移动到这个方法,2)调用onCreate()内部的方法,然后调用该方法在案件里面......我想我可以做到
    • 这正是我想要的行为......我还有一个问题,我可以将多个片段事务放在一个方法中,还是需要为每个事务创建一个方法?
    • @NAYIR55 您可以将任何您想要的内容放入方法中。如果您还有其他需要帮助的问题,您应该发布一个新问题。
    【解决方案3】:

    我会回答我自己的问题

    这是我正在寻找的期望行为的另一种方法

    Change a fragment

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 2019-03-29
      • 2011-08-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多