Android-默认选中单选按钮时,如何正确显示片段?
我正在尝试使用片段制作应用程序,基于以下文档: 据我所知,我需要:Android-默认选中单选按钮时,如何正确显示片段?,android,android-fragments,radio-button,Android,Android Fragments,Radio Button,我正在尝试使用片段制作应用程序,基于以下文档: 据我所知,我需要: 主机活动[完成]: 这是我的主机活动(.java) 这是我的主机XML布局: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http
- 主机活动[完成]:
<?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>
<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>
这是我的片段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>
<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>
所以。。。我的问题是:
使用单选组和单选按钮,我设置了一个默认选中的单选按钮,当我打开包含单选按钮和片段的主机活动时,单选按钮被选中,但在我再次选中按钮之前它不会显示片段,如果我使用
如果语句是相同的结果,我做错了什么 发生的情况是,默认情况下,您正在检查收音机,并且只有在更改或按下收音机时才加载片段。我建议在运行时加载默认片段,以避免点击按钮使其显示。目前,您只加载一个片段以响应onCheckedChanged()中的事件。您还需要在onCreate()
中加载适当的片段。首先,您可能希望复制并粘贴代码,将片段直接加载到onCreate()
中。最后,您可以通过将片段加载代码放入方法(即,loadFragment()
)中来减少重复代码,该方法是从onCreate()
和onCheckedChanged()
调用的。我将回答自己的问题
这是我一直在寻找的另一种实现理想行为的方法
希望这有帮助这意味着。。。在主机活动内的XML中插入片段,然后使用开关循环其他两个不起作用的片段???@NAYIR55。如果在XML中静态指定片段,则无法使用FragmentTransaction
以编程方式更改它。您能告诉我如何更改吗?我的编码技巧有些基础,所有的代码都是基于SO和Android开发者的教程和答案docs@NAYIR55您已经具有所需的构建块。诀窍是把它们按正确的顺序放在一起。看看你能不能从我写的东西中找到答案。当你试过之后,再来问一些问题。只是为了确定。。。1) 创建一个新方法来加载片段:将代码从case移动到此方法,2)在onCreate()中调用该方法,然后在case中调用该方法。。。我想我能做到这正是我想要的行为。。。我还有一个问题,我可以在一个方法中放入多个fragmenttransactions,还是需要为每个事务创建一个方法?@NAYIR55您可以在一个方法中放入任何您想要的内容。如果你有其他需要帮助的事情,你应该发布一个新问题。
<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>
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;
}
}
});