gpt4 book ai didi

Android - 默认选中单选按钮时如何正确显示 fragment ?

转载 作者:行者123 更新时间:2023-11-29 19:32:12 26 4
gpt4 key购买 nike

我正在尝试根据以下文档使用 fragment 制作应用:Fragments .

据我所知,我需要:

  • 主持 Activity [完成]:

这是我的主机 Activity (.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>
  • fragment [完成]:

这是我的 fragment .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;
}

}

这是我的 fragment 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 [完成]:

在我的主机 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>
  • 一种动态使用 fragment 的方法[完成]:

在这里,我尝试使用单选按钮和开关动态地使用 fragment :

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

所以...我的问题是:

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

最佳答案

目前,您只是加载一个 fragment 以响应 onCheckedChanged() 中的事件。您还需要在 onCreate() 中加载适当的 fragment 。首先,您可能只想复制并粘贴将 fragment 直接加载到 onCreate() 中的代码。最后,您可以通过将 fragment 加载代码放在一个方法(即 loadFragment())中来减少重复代码,该方法从 onCreate()onCheckedChanged( )

关于Android - 默认选中单选按钮时如何正确显示 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com