且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-11-20 09:51:40

我我认为如果你稍微改变点击次数会更容易。 lambda表达式部分仅适用于Android Studio 2.1及更高版本。

I think it would be easier if you handle the clicks a little differently. The lambda expression part only works in Android Studio 2.1 and above.

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

然后你的处理程序类有:

And then your handler class has:

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中,您将能够执行此操作:

Alternatively, you can use full-blown two-way binding. In the forthcoming Android Studio 2.2, you'll be able to do this:

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

然后你的处理程序类有:

And then your handler class has:

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),您需要为整数到TextView文本添加自己的双向绑定:

The trick added in Android Studio 2.2 is the support for the conversions for string concatenation with empty string. It is a useful shortcut. Without that (Android Studio 2.1), you'll need to add your own two-way binding for integer-to-TextView text:

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

这里是一个简化的绑定适配器。如果需要将文本观察者添加到同一TextView中,请使用TextViewBindingAdapter.java中的一个作为模板:

and here's a simplified binding adapter. Use the one in TextViewBindingAdapter.java as a template if you need to add text watchers to the same TextView:

@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);
    }
 }

请注意,我还没有编译任何这个,所以可能存在拼写错误。

Note that I haven't compiled any of this, so there may be typos.