使用单选按钮控件,完成性别选择,并将选择的结果以消息对话框形式进行显示,写出主要代码。
时间: 2024-12-13 20:17:23 浏览: 3
在Android开发中,你可以使用RadioGroup和RadioButton来创建性别选择单选按钮,并通过AlertDialog显示用户的选择。以下是一个简单的例子,假设你有两个RadioButton分别代表男性和女性:
首先,在`activity_main.xml`布局文件中添加RadioGroup和两个RadioButton:
```xml
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<Button
android:id="@+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认并显示结果"/>
</LinearLayout>
```
然后在`MainActivity.kt` (如果使用Kotlin) 或 `MainActivity.java` (如果使用Java) 中处理RadioGroup和确认按钮点击事件:
```kotlin
// Java 示例:
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
class MainActivity : AppCompatActivity() {
private lateinit var radioGroup: RadioGroup
private lateinit var radioButtonMale: RadioButton
private lateinit var radioButtonFemale: RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
radioGroup = findViewById(R.id.radioGroup)
radioButtonMale = findViewById(R.id.radioMale)
radioButtonFemale = findViewById(R.id.radioFemale)
btnConfirm.setOnClickListener {
val selectedButton = findViewById<Button>(R.id.btnConfirm)
if (radioButtonMale.isChecked) {
Log.d("Result", "选择: 男")
Toast.makeText(this, "选择的是男性", Toast.LENGTH_SHORT).show()
} else if (radioButtonFemale.isChecked) {
Log.d("Result", "选择: 女")
Toast.makeText(this, "选择的是女性", Toast.LENGTH_SHORT).show()
} else {
Log.d("Result", "未选择任何选项")
Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show()
}
}
}
}
// Kotlin 示例:
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RadioGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var radioGroup: RadioGroup
private lateinit var radioButtonMale: RadioButton
private lateinit var radioButtonFemale: RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
radioGroup = findViewById(R.id.radioGroup)
radioButtonMale = findViewById(R.id.radioMale)
radioButtonFemale = findViewById(R.id.radioFemale)
btnConfirm.setOnClickListener {
val selectedButton = findViewById<Button>(R.id.btnConfirm)
val checkedRadioButton = radioGroup.checkableButtons.firstOrNull { it.isChecked }
when (checkedRadioButton) {
radioButtonMale -> {
Toast.makeText(this, "选择的是男性", Toast.LENGTH_SHORT).show()
showAlertDialog("性别选择", "您选择了男性")
}
radioButtonFemale -> {
Toast.makeText(this, "选择的是女性", Toast.LENGTH_SHORT).show()
showAlertDialog("性别选择", "您选择了女性")
}
else -> {
Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun showAlertDialog(title: String, message: String) {
AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定") { _, _ ->
Toast.makeText(this, "对话框关闭", Toast.LENGTH_SHORT).show()
}
.NegativeButton("取消", DialogInterface.OnClickListener { _, _ ->
Toast.makeText(this, "取消操作", Toast.LENGTH_SHORT).show()
})
.show()
}
}
```
阅读全文