布局使用数据绑定将android中的整数值限制从0到10绑定 [英] Layout Binding integer value limit from 0 to 10 in android with databinding

查看:313
本文介绍了布局使用数据绑定将android中的整数值限制从0到10绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在数据绑定的帮助下在android中设置最小值为0且最大值为10的整数限制。
为此,我有一个可绑定的适配器,它设置一个带两个监听器的整数值,一个增加值,另一个减少它。现在最后我想设置该整数的限制,最小值为0,最大值为10。

I am trying to set the limit of an integer with a minimum value of 0 and maximum of 10 in android with the help of databinding. For that i have a bindable adapter which set the value of an integer with two listener one increase the value and the other decrease it. Now finally i wanted to set the limit of that integer, minimum of 0 and maximum of 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);
    }
}


推荐答案

我我认为如果你稍微改变点击次数会更容易。 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.

这篇关于布局使用数据绑定将android中的整数值限制从0到10绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆