【发布时间】:2021-02-01 02:51:41
【问题描述】:
所以我在 Kotlin 编程语言中实现了 RadioGroup 和 RadioButton 的简单工作原理,我使用 setOnClickListener 方法让我的应用可以像其他应用一样点击,我使用 Fragment 来实现我的简单 RadioGroup 和 RadioButton 应用。
当我为此方法传递 SetOnclick 时:
submitButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val selectedId = surveyRadioGroup.checkedRadioButtonId
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
Log.d("TEST", selectedRadioButton.text.toString())
dismiss()
}
我出错了。
上面写满了我的代码:
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.fragment.app.DialogFragment
class MyFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView: View = inflater.inflate(R.layout.dialog_fragment, container, false)
val cancelButton = rootView.findViewById<Button>(R.id.cancelButton)
var submitButton = rootView.findViewById<Button>(R.id.submitButton)
var surveyRadioGroup = rootView.findViewById<RadioGroup>(R.id.myradiogroup)
cancelButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
dismiss()
}
})
submitButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val selectedId = surveyRadioGroup.checkedRadioButtonId
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
Log.d("TEST", selectedRadioButton.text.toString())
dismiss()
}
})
return rootView
}
}
这是我用于连接 XML 类和 Kotlin 类的 dialog_fragment.xml 上面的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<RadioGroup
android:id="@+id/myradiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Exellent"
android:layout_marginTop="30dp"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Very GOOD"
tools:ignore="ObsoleteLayoutParam"
android:layout_marginTop="60dp"/>
<RadioButton
android:text="Good"
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginTop="90dp"/>
<RadioButton
android:text="Average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/radioButton4"
android:layout_marginTop="120dp"/>
<RadioButton
android:text="Bad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/radioButton5"
android:layout_marginTop="150dp"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/submitButton"
android:layout_marginTop="200dp"
/>
<Button
android:text="cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/cancelButton"
android:layout_marginTop="270dp"
/>
</RelativeLayout>
</RelativeLayout>
此应用为 Androidx 创建了需求
【问题讨论】:
标签: android xml kotlin radio-button radio-group