如何使用 RxJava 绑定单选按钮 [英] How to bind Radio Buttons using RxJava
问题描述
我正在关注 Qiitanium 应用程序(请参阅链接中突出显示的行),但我无法弄清楚如何绑定 RadioButtons
I'm following the code of the Qiitanium app (See the highlighted lines in link) and I have trouble figuring out how I can bind RadioButtons
假设我有一个以 R.id.rgMyButtons 作为 Id 的 RadioGroup,它包含 3 个 RadioButtonsDead"、Alive"、Body Missing",ID 为 R.id.rbDead、R.id.rbAlive、R.id.rb 缺失
Say I have a RadioGroup with R.id.rgMyButtons as Id and it contains 3 RadioButtons "Dead", "Alive", "Body Missing" with Ids are R.id.rbDead, R.id.rbAlive, R.id.rbMissing
我将 RadioGroup 字段设置为
I set the RadioGroup field as
Rx<RadioGroup> radioGroup;
我在 onViewCreated 中将视图设置为
I get the view set in onViewCreated as
rdGroup = RxView.findById(this, R.id.rgMyButtons);
rdGroup.get().setOnCheckedChangeListener(mOnPersonStateUpdateListener);
在 onBind 中,我想将 RadioGroup 绑定到模型数据,以便返回的值直接映射到该组中正确的 RadioButton.我正在寻找类似的东西
In onBind, I'd like to bind the RadioGroup to the model data so that the value which returns maps directly to the correct RadioButton in that group. I'm looking for something like
rdGroup.bind(person.state(), RxActions.someAction()),
以便绑定到 RadioGroup 并自动设置正确的值.
So that it is bound to the RadioGroup and automatically sets the correct value.
person_state() 实际上为 Dead 返回 2,为 Alive 返回 3,为 Missing 返回 4,我希望它检查 RadioGroup 中的正确字段.我如何在 RxAndroid 中实现这一点?
The person_state() actually returns 2 for Dead, 3 for Alive and 4 for Missing and I want that to check to the correct field in RadioGroup. How can I achieve this in RxAndroid?
推荐答案
我能够自己解决这个问题.这是任何可能遇到相同问题的人的答案......
I was able to solve this on my own. Here's the answer for anyone who might come across the same issue....
我在类中写了一个函数
protected RxAction<RadioGroup, String> setRadioButton() {
return new RxAction<RadioGroup, String>() {
@Override
public void call(final RadioGroup radioGroup, final String selection) {
RadioButton rb;
switch(selection)
{
case "2":
rb = (RadioButton)findViewById(R.id.rbDead);
rb.setChecked(true);
break;
case "3":
rb = (RadioButton)findViewById(R.id.rbAlive);
rb.setChecked(true);
break;
case "4":
rb = (RadioButton)findViewById(R.id.rbMissing);
rb.setChecked(true);
break;
}
}
};
}
在我使用的 onBind 里面
And inside the onBind I used
rdGroup.bind(person.state(), setRadioButton()),
这篇关于如何使用 RxJava 绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!