如何在Android中验证性别的单选按钮?

gxwragnw  于 2023-06-20  发布在  Android
关注(0)|答案(4)|浏览(177)

大家好,我如何验证我的表单中的“性别”,我使用了RadioButton,这样当选择**“男性”时,“女性”将自动取消选择。同样,我必须使用RadioButton还是使用RadioGroup**更好?谢谢

jfewjypa

jfewjypa1#

将您的性别RadioButton s放入RadioGroup

6jjcrrmo

6jjcrrmo2#

使用RadioGroup。您可以查看完整文档RadioGroup
您可以从developer.android.com-> http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons
例如,在xml文件中:

  1. <RadioGroup android:layout_height="wrap_content" android:id="@+id/gender_group"
  2. android:layout_width="match_parent">
  3. <RadioButton android:text="Male"
  4. android:layout_width="wrap_content" android:id="@+id/radio0"
  5. android:layout_height="wrap_content" android:checked="true"></RadioButton>
  6. <RadioButton android:text="Female"
  7. android:layout_width="wrap_content" android:id="@+id/radio1"
  8. android:layout_height="wrap_content"></RadioButton>
  9. </RadioGroup>
x7yiwoj4

x7yiwoj43#

试试这个

  1. public void onRadioButtonClicked(View view) {
  2. // Is the button now checked?
  3. boolean checked = ((RadioButton) view).isChecked();
  4. // Check which radio button was clicked
  5. switch(view.getId()) {
  6. case R.id.radio_pirates:
  7. if (checked)
  8. // Pirates are the best
  9. break;
  10. case R.id.radio_ninjas:
  11. if (checked)
  12. // Ninjas rule
  13. break;
  14. }
  15. }
展开查看全部
klsxnrf1

klsxnrf14#

请使用Kotlin-

  1. // Validate the gender selection
  2. if (!maleRadioButton.isChecked && !femaleRadioButton.isChecked) {
  3. Toast.makeText(this, "Please select your gender", Toast.LENGTH_SHORT).show()
  4. return
  5. }
  6. For Better Understanding you can see this demo code -
  7. class ThirdActivity : AppCompatActivity() {
  8. private lateinit var maleRadioButton :RadioButton
  9. private lateinit var femaleRadioButton : RadioButton
  10. override fun onCreate(savedInstanceState: Bundle?) {
  11. super.onCreate(savedInstanceState)
  12. setContentView(R.layout.activity_third)
  13. // Enable the back button in the action bar
  14. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  15. val button = findViewById<Button>(R.id.buttonSendData)
  16. //Radiobutton
  17. maleRadioButton = findViewById(R.id.radioMale)
  18. femaleRadioButton = findViewById(R.id.radioFemale)
  19. button.setOnClickListener {
  20. sendDataToFourthActivity()
  21. }
  22. }
  23. private fun sendDataToFourthActivity() {
  24. //set userName
  25. val editText = findViewById<EditText>(R.id.userName)
  26. val username = editText.text.toString()
  27. //set userEmail
  28. val editText1 = findViewById<EditText>(R.id.userEmail)
  29. val userEmail = editText1.text.toString()
  30. //set userAge
  31. val editText2 = findViewById<EditText>(R.id.userAge)
  32. val userAge = editText2.text.toString()
  33. //set userMobile
  34. val editText3 = findViewById<EditText>(R.id.userMobile)
  35. val userMobile = editText3.text.toString()
  36. // Validate the form
  37. if(username.isBlank() || userEmail.isBlank() || userAge.isBlank() ||userMobile.isBlank())
  38. {
  39. Toast.makeText(this,"Please fill all the details", Toast.LENGTH_SHORT)
  40. .show()
  41. return
  42. }
  43. // Validate the gender selection
  44. if (!maleRadioButton.isChecked && !femaleRadioButton.isChecked) {
  45. Toast.makeText(this, "Please select your gender", Toast.LENGTH_SHORT).show()
  46. return
  47. }
  48. //Pass the RadioButton data
  49. val intent = Intent(this, FourthActivity::class.java)
  50. if (maleRadioButton.isChecked) {
  51. intent.putExtra("gender", "Male")
  52. } else if (femaleRadioButton.isChecked) {
  53. intent.putExtra("gender", "Female")
  54. }
  55. intent.putExtra("userName", username)
  56. intent.putExtra("userEmail", userEmail)
  57. intent.putExtra("userAge", userAge)
  58. intent.putExtra("userMobile", userMobile)
  59. startActivity(intent)
  60. }
  61. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  62. // Handle back button clicks
  63. if (item.itemId == android.R.id.home) {
  64. onBackPressed()
  65. return true
  66. }
  67. return super.onOptionsItemSelected(item)
  68. }
  69. }
展开查看全部

相关问题