【发布时间】: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