如何将检查的单选按钮值传递到Kotlin Android Studio中的文本视图中?

发布于 2025-02-12 13:01:20 字数 78 浏览 0 评论 0原文

我制作了一个对话框,然后将广播组放入其中,然后添加三个无线电按钮1)男性2)女性3)如果用户选择男性,则应在文本视图中显示选定的无线电按钮男性

I made a dialogue box and I put radio group in it and I add three radio button 1)male 2)female 3)others if user select male so the selected radio button male should be shown in text View

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2025-02-19 13:01:20

在这里,我想在这里与您分享一个简单的应用程序,并同时使用活动代码和布局代码。我希望这对您有帮助。

首先,布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="100dp"
    tools:context=".TestActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:text="What is your gender?"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtViewGender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:text="-- Selected Gender --"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select" />

</LinearLayout>

第二个主要活动代码

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class GenderActivity: AppCompatActivity() {
    private lateinit var selectedGender: String
    private var selectedGenderIndex: Int = 0
    private val gender = arrayOf("Male", "Female", "Others")
    private lateinit var txtViewGender: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test4)

        txtViewGender = findViewById<TextView>(R.id.txtViewGender)
        var androidButton: Button = findViewById<Button>(R.id.button)
        androidButton.setOnClickListener {
            showRadioDialog()
        }
    }

    private fun showRadioDialog() {
        selectedGender = gender[selectedGenderIndex]
        MaterialAlertDialogBuilder(this)
            .setTitle("Select your gender")
            .setSingleChoiceItems(gender, selectedGenderIndex) { dialog, which ->
                selectedGenderIndex = which
                selectedGender = gender[which]
            }
            .setPositiveButton("Ok") { dialog, which ->
                Toast.makeText(this, "Selected --> $selectedGender ", Toast.LENGTH_SHORT)
                    .show()
                txtViewGender.text = selectedGender
            }
            .setNegativeButton("Cancel") { dialog, which ->
                dialog.dismiss()
            }
            .show()

    }
}

在运行代码时,您将获得以下两个屏幕。

屏幕1

”

屏幕2

”屏幕2“

Here, I would like to share you here a simple app with both the activity and layout code. I hope this will help you.

First the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="100dp"
    tools:context=".TestActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:text="What is your gender?"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtViewGender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:text="-- Selected Gender --"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select" />

</LinearLayout>

Second the Main Activity code

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class GenderActivity: AppCompatActivity() {
    private lateinit var selectedGender: String
    private var selectedGenderIndex: Int = 0
    private val gender = arrayOf("Male", "Female", "Others")
    private lateinit var txtViewGender: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test4)

        txtViewGender = findViewById<TextView>(R.id.txtViewGender)
        var androidButton: Button = findViewById<Button>(R.id.button)
        androidButton.setOnClickListener {
            showRadioDialog()
        }
    }

    private fun showRadioDialog() {
        selectedGender = gender[selectedGenderIndex]
        MaterialAlertDialogBuilder(this)
            .setTitle("Select your gender")
            .setSingleChoiceItems(gender, selectedGenderIndex) { dialog, which ->
                selectedGenderIndex = which
                selectedGender = gender[which]
            }
            .setPositiveButton("Ok") { dialog, which ->
                Toast.makeText(this, "Selected --> $selectedGender ", Toast.LENGTH_SHORT)
                    .show()
                txtViewGender.text = selectedGender
            }
            .setNegativeButton("Cancel") { dialog, which ->
                dialog.dismiss()
            }
            .show()

    }
}

When you run the code, you will get the following two screens.

Screen 1

Screen1

Screen 2

Screen 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文