根据单选按钮选择显示剩余视图

Show remaining views depending on radio button selection

我正在开发一个 android 应用程序,其中有一个单选按钮,并且根据单选按钮的选择,我想显示两组输入视图中的任何一个,即如果单选按钮 1 是选择后,我想显示一组不同的输入视图,如果选择了单选按钮 2,我想显示一组不同的输入视图。

到目前为止,我所做的是,在我的 activity 中,我有一个单选按钮,然后我创建了两组视图(但不可见),当其中一个单选按钮是选择后,我使一组视图可见。

我认为这不是执行此操作的“标准”方法,因此请寻求帮助了解在这种情况下什么是理想的?

我是否可以更改工作流程以根据选中的单选按钮打开一个新的 activity?

最好的方法是将每个布局及其逻辑分离到单独的文件中。

最好的解决方案是使用 Fragments,每个片段都有自己的布局 xml 文件和 Kotlin/Java class 用于编码。

实施

首先,您必须创建两个片段。

FirstFragment.kt

class FirstFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_first, container, false)
    }
}

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#336699"
    tools:context=".FirstFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="First Fragment"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold"/>

</FrameLayout>

SecondFragment.kt

class SecondFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_second, container, false)
    }
}

fragment_second.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#996633"
    tools:context=".SecondFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Second Fragment"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold"/>

</FrameLayout>

现在,在您的 activity 中,您需要使用两个 RadioButtons 创建 RadioGroup,然后添加 FragmentContainerView,它将成为所有片段的容器。

activity_main.xml

<androidx.constraintlayout.widget.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=".MainActivity">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="#cccccc"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <RadioButton
            android:id="@+id/radio_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View 1"/>

        <RadioButton
            android:id="@+id/radio_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="View 2"/>

    </RadioGroup>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/radio_group"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

最后一步是在用户更改 RadioButton 选择时实现 activity 中的片段更改。

MainActivity.kt

class MainActivity : AppCompatActivity() {

    lateinit var radioGroup : RadioGroup

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        radioGroup = findViewById(R.id.radio_group)
        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId){
                R.id.radio_1 -> supportFragmentManager.beginTransaction().replace(R.id.fragment_container, FirstFragment()).commit()
                R.id.radio_2 -> supportFragmentManager.beginTransaction().replace(R.id.fragment_container, SecondFragment()).commit()
            }
        }
        radioGroup.check(R.id.radio_1) // To check the first radio by default
    }
}

结果

  • 当第一个RadioButton被选中时

  • 当第二个RadioButton被选中时