Android RadioButton,使用Kotlin的RadioGroup
在本教程中,我们将使用Kotlin在Android应用程序中讨论和实现RadioButton和RadioGroup。
Android单选按钮
RadioButton是可以设置为选中或者未选中状态的小部件。
选中RadioButton后,除非RadioGroup内部存在该按钮,否则无法取消选中它。
RadioGroup是保存RadioButtons的容器。
一次在RadioGroup中,只能将一个RadioButton设置为选中状态。
通过以下方式在xml中定义RadioButton:
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="androidlyRadioButton"
android:text="Androidly RadioButtons"
android:textSize="18sp"
android:text用于设置RadioButton的文本。
您可以使用[android:gravity]属性设置文本重力[android:onClick]用于设置Kotlin活动中的功能,以在单击RadioButton时触发该功能。
android:buttonTint用于设置圆形按钮的颜色。
默认情况下,它设置为styles.xml中指定的colorAccent。
要以编程方式定义RadioButton,请使用:
val radioButton = RadioButton(this)
在XML中以以下方式定义RadioGroup。
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Layouts"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="androidlyRadioButton"
android:text="Androidly RadioButtons"
android:textSize="18sp"
.
.
.
.
</RadioGroup>
在RadioGroup上设置方向将按该顺序(水平/垂直)放置RadioButton。
要以编程方式定义RadioGroup,我们需要执行以下操作:
val radioButton = RadioButton(this) val radioGroup = RadioGroup(this) radioGroup.addView(radioButton)
这将在RadioGroup内部添加单个RadioButton。
RadioGroup可以设置类似于LinearLayout的布局权重。
我们在RadioGroup上使用属性android:weightSum,在RadioButton上使用属性android:layout_weight。
要从RadioGroup清除所有状态,我们需要在Kotlin Activity类中调用以下内容。
radioGroup.clearCheck()
RadioGroup侦听器在我们的活动中,我们可以使用RadiooGroup.OnCheckChangedListener接口回调来侦听RadiooGroup内部保存的RadiooButtons状态的变化。
radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { radioGroup, i ->
textView.text = "option "+i+" is selected"
})
radioGroup参数是当前的无线电组,而i是该RadioGroup中存在的RadioButton的ID。
在下一节中,我们将创建一个Single View应用程序,该应用程序可以通过XML以及以编程方式托管RadioGroup。
只要选中任何一个单选按钮,我们就会显示一个Toast。
布局代码
下面给出了activity_main.xml布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/androidlyRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="androidlyRadioButton"
android:text="Androidly RadioButtons"
android:textSize="18sp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Which layout has child views on top of each other?"
android:textSize="20sp"
<RadioGroup
android:id="@+id/firstRg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Layouts"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LinearLayout"
android:textSize="18sp"
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="RelativeLayout"
android:textSize="18sp"
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FrameLayout"
android:textColor="#99cc00"
android:textSize="18sp"
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TableLayout"
android:textSize="18sp"
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Which of the following are clickable?"
android:textSize="20sp"
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Groups"
</LinearLayout>
活动代码
MainActivity.kt类的代码如下:
package net.androidly.androidlyradiobuttons
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity(), RadioGroup.OnCheckedChangeListener {
val buttonTexts = arrayListOf("Buttons", "Text", "Both")
val ID = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val secondRg = RadioGroup(this)
secondRg.orientation = RadioGroup.HORIZONTAL
secondRg.weightSum = 3f
secondRg.id = ID
secondRg.contentDescription = "Widgets"
secondRg.setOnCheckedChangeListener(this)
linearLayout.firstRg.setOnCheckedChangeListener(this)
val p = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
p.weight = 0.5f
for (i in 0 until buttonTexts.size) {
val radioButton = RadioButton(this)
radioButton.apply {
text = buttonTexts[i]
id = i
layoutParams = p
}
secondRg.addView(radioButton)
}
linearLayout.addView(secondRg, 4)
button.setOnClickListener {
secondRg.clearCheck()
linearLayout.firstRg.clearCheck()
}
}
override fun onCheckedChanged(group: RadioGroup?, checkId: Int) {
val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
checkedRadioButton?.let {
if (checkedRadioButton.isChecked)
Toast.makeText(applicationContext, "RadioGroup: ${group?.contentDescription} RadioButton: ${checkedRadioButton?.text}", Toast.LENGTH_LONG).show()
}
}
fun androidlyRadioButton(view: View) {
val radioButton = view as RadioButton
Toast.makeText(applicationContext, "Radio Button: ${radioButton.text}", Toast.LENGTH_LONG).show()
}
}
在上面的代码中,我们创建了第二个RadioGroup,它水平放置RadioButtons。
我们已经在Activity中实现了" RadioGroup.OnCheckedChangeListener"接口。
当检查布局中定义的单个RadioButton时,将触发androidlyRadioButton。
我们需要将其从View转换为RadioButton。
" fun onCheckedChanged(group:RadioGroup ?, checkId:Int)"是每当任何RadioGroups中的RadioButton被选中或者未选中时触发的东西。checkedRadioButtonId属性用于获取选中的RadioButton的ID。
我们仅在选中单选按钮时使用RadioButton的isChecked属性来显示Toast。

