问题描述
因此,我实现了一个简单的RadioGroup和RadioButton如何以Kotlin编程语言工作的方法,并且我使用setonClickListener
方法使我的应用程序可以像其他应用程序一样单击,并且我使用Fragment来实现我的简单RadioGroup和RadioButton应用程序。
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
}
}
这是我的dialog_fragment.xml,用于连接XML类和Kotlin类上述代码。
<?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的要求
解决方法
selectedRadioButton
为空的原因是:
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
在onCreateView()
内部被调用,即此时尚未创建rootView
。
改为在onViewCreated()
中设置视图。