【问题标题】:Show remaining views depending on radio button selection根据单选按钮选择显示剩余视图
【发布时间】:2022-01-15 16:28:09
【问题描述】:

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

Till now what I've done is that in my activity, I have a radio button, and then I am creating both set of views (but not visible), and when either of radio button is selected, I make one set可见的视图。

我认为这不是“标准”的做法,所以在这种情况下寻求帮助?

我是否应该根据选择的单选按钮更改工作流程以打开新活动?

【问题讨论】:

  • 你试过 view.setVisibility(View.GONE) 吗?使用 View.GONE 完全删除视图,这样它就不会占用屏幕空间。
  • 您可以使用片段来实现此行为,它将“选择”作为参数并显示它们。然后,您可以根据选择单选按钮创建一个具有所需选项的新片段实例。
  • 如果您使用约束布局,则简单方法将所有视图映射到 2 组组中,如果您不使用约束布局,则选择单选按钮设置组的可见性以显示和隐藏,您可以创建具有视图组小部件(如线性布局或相对)的组并显示和隐藏它们,通过设置 1 个视图,所有视图都将被隐藏或显示

标签: android layout


【解决方案1】:

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

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

实施

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

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>

现在,在您的活动中,您需要创建带有两个RadioButtonsRadioGroup,然后添加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中实现fragment更改。

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被选中时

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 2018-02-27
    • 2010-11-05
    • 2015-11-26
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多