【问题标题】:How to check which radio button is selected in kotlin?如何检查在kotlin中选择了哪个单选按钮?
【发布时间】:2022-09-22 22:20:33
【问题描述】:

我正在尝试制作一个待办事项应用程序,并将单选组设置为具有高、中和低优先级的单选按钮。但是,每次都出现不同的错误。我正在尝试根据这个点击的单选按钮将优先级以字符串的形式打印到房间数据库,例如,如果用户点击高优先级单选按钮,它将在房间数据库的优先级列中写入高。我怎样才能做到这一点。

添加笔记片段

  private var fragmentAddNoteBinding:FragmentAddNoteBinding? = null
    private lateinit var viewModel:AddNoteViewModel

    private val sdf = SimpleDateFormat(\"yyyy-MM-dd\")
    private val currentDate = sdf.format(Date())
   

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity()).get(AddNoteViewModel::class.java)

        val binding =FragmentAddNoteBinding.bind(view)
        fragmentAddNoteBinding = binding


        binding.AddNoteFragmentBtn.setOnClickListener{

            viewModel.makeNote(
                binding.AddNoteFragmentTxtTitle.text.toString(),
                binding.AddNoteFragmentTxtNote.text.toString(),
                currentDate
                // priority <-- here I want to send priority as string like this


            )
        }


    }

添加NoteViewModel

@HiltViewModel
class AddNoteViewModel @Inject constructor(
    private val repository: INoteRepository,

): ViewModel() {



    private fun insertNote(note: Note) = viewModelScope.launch{
        repository.Insert(note)
    }


    fun makeNote(title:String,note:String,currentDate:String){ //here I will take the priority as a parameter and create a new note and save it to room
        if(title.isEmpty() || note.isEmpty()){
            println(\"Enter titel,note,priority\")
            return
        }


        val note = Note(note,title,currentDate)
        insertNote(note)

    }



}

我的广播组 xml

    <RadioGroup

        android:id=\"@+id/AddNoteFragmentRadioGroup\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:layout_margin=\"8dp\"
        android:orientation=\"horizontal\">

        <RadioButton
            android:id=\"@+id/AddNoteFragmentHighPriorityRadioBtn\"
            android:layout_width=\"wrap_content\"
            android:layout_height=\"wrap_content\"
            android:layout_margin=\"8dp\"></RadioButton>

        <RadioButton
            android:id=\"@+id/AddNoteFragmentMediumPriorityRadioBtn\"
            android:layout_width=\"wrap_content\"
            android:layout_height=\"wrap_content\"
            android:layout_margin=\"8dp\"></RadioButton>

        <RadioButton
            android:id=\"@+id/AddNoteFragmentLowPriorityRadioBtn\"
            android:layout_width=\"wrap_content\"
            android:layout_height=\"wrap_content\"
            android:layout_margin=\"8dp\"></RadioButton>
    </RadioGroup>

    标签: android kotlin


    【解决方案1】:

    单选组视图使用功能:

    getCheckedRadioButtonId()
    

    前任:

      val rbId = binding.AddNoteFragmentRadioGroup.getCheckedRadioButtonId()
    when (rbId) {
        R.id.AddNoteFragmentHighPriorityRadioBtn->print("1")
        R.id.AddNoteFragmentMediumPriorityRadioBtn->print("2")
        R.id.AddNoteFragmentLowPriorityRadioBtn->print("3")
    }
    

    【讨论】:

    • 你能举个例子我如何将它应用到我自己的代码中吗?
    • 我更新了我的答案,请检查
    • 多谢。有用
    • 没关系))))
    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2019-01-13
    • 2022-12-30
    • 2011-07-12
    • 1970-01-01
    相关资源
    最近更新 更多