【问题标题】:How to bind Radio Buttons using RxJava如何使用 RxJava 绑定单选按钮
【发布时间】:2015-05-12 01:51:17
【问题描述】:

我正在关注Qiitanium 应用程序的代码(请参阅链接中突出显示的行),但我无法弄清楚如何绑定 RadioButtons

假设我有一个 R.id.rgMyButtons 作为 Id 的 RadioGroup,它包含 3 个 RadioButtons “Dead”、“Alive”、“Body Missing”,ID 分别是 R.id.rbDead、R.id.rbAlive、R。 id.rbMissing

我将 RadioGroup 字段设置为

Rx<RadioGroup> radioGroup;

我将 onViewCreated 中的视图设置为

rdGroup = RxView.findById(this, R.id.rgMyButtons);
rdGroup.get().setOnCheckedChangeListener(mOnPersonStateUpdateListener);

在 onBind 中,我想将 RadioGroup 绑定到模型数据,以便返回的值直接映射到该组中正确的 RadioButton。我正在寻找类似的东西

rdGroup.bind(person.state(), RxActions.someAction()),

使其绑定到 RadioGroup 并自动设置正确的值。

person_state() 实际上返回 2 代表死亡,3 代表活着,4 代表失踪,我希望检查 RadioGroup 中的正确字段。如何在 RxAndroid 中实现这一点?

【问题讨论】:

    标签: android rx-java radio-group rx-android


    【解决方案1】:

    我能够自己解决这个问题。这是任何可能遇到相同问题的人的答案....

    我在类中写了一个函数为

    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 内部

    rdGroup.bind(person.state(), setRadioButton()),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2017-02-12
      • 2011-01-20
      • 2012-02-12
      • 2015-10-31
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多