当前位置: 首页 > 知识库问答 >
问题:

使用数据绑定的android中布局绑定整数值限制为0到10

吴鸿禧
2023-03-14

我正试图通过数据绑定来设置android中最小值为0、最大值为10的整数的限制。为此,我有一个可绑定的适配器,它通过两个侦听器设置一个整数的值,一个增加值,另一个减少值。最后,我想设置这个整数的极限,最小值为0,最大值为10。

@BindingAdapter("quantity")
public static void setQuantityText(TextView textView, int quantity) {
    textView.setText(String.valueOf(quantity));
}
public static class ListenerIncrease implements View.OnClickListener {

    private FragmentBinding binding;

    public ListenerIncrease(FragmentBinding binding) {
        this.binding = binding;
    }

    @Override
    public void onClick(View v) {
        int quantity = binding.getQuantity();
        binding.setQuantity(++quantity);
    }
}
public static class ListenerDecrease implements View.OnClickListener {

    private FragmentBinding binding;

    public ListenerDecrease(FragmentBinding binding) {
        this.binding = binding;
    }
    @Override
    public void onClick(View v) {
        int quantity = binding.getQuantity();
        binding.setQuantity(--quantity);
    }
}

共有1个答案

墨翔宇
2023-03-14

我认为如果你处理点击的方式有点不同,会更容易。lambda表达式部分仅适用于Android Studio 2.1及以上版本。

<Button android:onClick="@{(view)->Handlers.increment(view, 10)}" .../>
<Button android:onClick="@{(view)->Handlers.decrement(view, 0)}" .../>
<TextView app:quantity="@{quantity}"/>

然后你的处理程序类有:

public static void increment(View view, int max) {
    FragmentBinding binding = DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
}

public static void decrement(View view, int min) {
    FragmentBinding binding = DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
}

或者,您可以使用完整的双向绑定。在即将发布的Android Studio 2.2中,您将能够做到:

<Button android:onClick="@{()->Handlers.increment(quantityView, 10)}" .../>
<Button android:onClick="@{()->Handlers.decrement(quantityView, 0)}" .../>
<TextView android:id="@+id/quantityView" app:quantity="@={`` + quantity}"/>

然后你的处理程序类有:

private static int getCurrentIntValue(TextView view) {
    try {
        return Integer.parseInt(view.getText().toString());
    } catch (NumberFormatException e) {
        return 0;
    }
}
public static void increment(TextView view, int max) {
    int value = getCurrentIntValue(view);
    binding.setQuantity(Math.max(max, value + 1));
}

public static void decrement(View view, int min) {
    int value = getCurrentIntValue(view);
    binding.setQuantity(Math.min(min, value - 1));
}

Android Studio 2.2中增加的技巧是支持字符串与空字符串连接的转换。这是一条有用的捷径。如果没有它(Android Studio 2.1),您需要为integer到TextView文本添加自己的双向绑定:

@InverseBindingAdapter(attribute = "quantity")
public static int getQuantity(TextView view) {
    return getCurrentIntValue(view);
}

这里有一个简化的绑定适配器。使用TextViewBindingAdapter中的一个。如果需要向同一文本视图添加文本监视程序,请使用java作为模板:

@BindingAdapter("onQuantityChanged")
public static void setQuanityWatcher(TextView view,
        final InverseBindingListener quantityChanged) {
    TextWatcher newTextWatcher;
    if (quantityChanged == null) {
        newTextWatcher = null;
    } else {
        newTextWatcher = new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                quantityChanged.onChange();
            }
            // others are empty...
        }
    }
    TextWatcher oldTextWatcher = ListenerUtil.trackListener(
        view, newTextWatcher, R.id.textWatcher);
    if (oldTextWatcher != null) {
        view.removeTextChangeListener(oldTextWatcher);
    }
    if (newTextWatcher != null) {
        view.addTextChangedListener(newTextWatcher);
    }
 }

注意,我没有编译过这些,所以可能有错别字。

 类似资料:
  • 我想包括一个布局与数据绑定。 我想使用将id从java传递到布局,但我似乎找不到正确的语法。 这是我的片段类,带有和: 我想充气。分割项目,并使其包含

  • 如何将MVVM值绑定到dropdownlist?下面的输入元素运行良好

  • Android提供了通过数据绑定在UI上显示数据的新概念。我试着在我的一个应用程序上实现它。在实现Lambda表达式作为按钮的单击处理程序时,我需要一个常量,即view.visible来在表达式中比较它。但是当我编写下面的代码时:

  • 在使用新的数据绑定api时,我发现无法绑定到“style”属性。编译器抱怨找不到样式。然而,如果我简单地将样式设置为原样,它会发现它很好。例如: 不工作: 作品: 错误: 错误:任务“:app:compiledBugJavaWithJavaC”的执行失败。 JAVAlang.RuntimeException:发现数据绑定错误。****/数据绑定错误****msg:标识符必须具有XML文件中的用户定

  • 我想知道以下内容之间有什么区别: 与 有任何性能差异吗? 每个的首选用例是什么? 任何其他信息都将不胜感激! 谢谢!

  • 主要内容:1 完整数据绑定的示例完整数据绑定是指将JSON映射到任何Java对象。 1 完整数据绑定的示例 1.1 编写核心类 MainApp: 1.2 运行测试 项目根目录下生成student.json文件,内容如下: