我在Binding Utils类中有这个适配器:
@BindingAdapter("text")
public static void bindDoubleInText(AppCompatEditText tv, Double
value)
{
if (tv.getText() == null) return;
// Get the actual EditText value
String edittextValue = tv.getText().toString();
if (edittextValue.isEmpty())
{
if (value != null) tv.setText(Double.toString(value));
}
else
{
if (value != null &&
Double.parseDouble(tv.getText().toString()) != value)
tv.setText(Double.toString(value));
}
}
@InverseBindingAdapter(attribute = "android:text")
public static Double getDoubleFromBinding(TextView view)
{
String string = view.getText().toString();
return string.isEmpty() ? null : Double.parseDouble(string);
}
@BindingAdapter("text")
public static void bindIntegerInText(AppCompatEditText
appCompatEditText, Integer value)
{
CharSequence charSequence = appCompatEditText.getText();
if (charSequence == null || value == null) return;
// Get the actual EditText value
String edittextValue = charSequence.toString();
if (edittextValue.isEmpty() || Integer.parseInt(edittextValue) !=
value)
appCompatEditText.setText(Integer.toString(value));
}
@InverseBindingAdapter(attribute = "android:text")
public static Integer getIntegerFromBinding(TextView view)
{
String string = view.getText().toString();
return string.isEmpty() ? null : Integer.parseInt(string);
}`
这是我的一个xml文件的一部分:
<android.support.v7.widget.AppCompatEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:hint="dosis"
android:inputType="numberDecimal"
android:minEms="5"
android:text="@={workOrderFertilizer.dose}"/>`
事情是在Android Studio 2.2.3版本中工作得很好。当我更新到Android Studio 2.3时,它停止工作,Gradle显示多个错误。我已将绑定适配器和反向绑定适配器的属性从“android:text”更改为“text”但它不起作用。在这个问题中,另一个用户发生了类似的事情:Data Binding broke after upgrade to Gradle 2.3但不同之处在于我使用“android:text”进行绑定,因此George Mount给出的答案对我没用。有人有什么想法吗?我对此感到非常沮丧,因为我无法更新Android Studio,因为这个问题。
答案 0 :(得分:2)
双向绑定的快捷方式(如@BindingAdapter("android:text")
public static void setDouble(TextView view, Double val) {
if (val != null) {
String currentValue = view.getText().toString();
if (currentValue.length() != 0) {
try {
double oldVal = Double.parseDouble(currentValue);
if (oldVal == val) {
return;
}
} catch (NumberFormatException e) {
// that's ok, we can just set the value.
}
}
view.setText(val.toString());
}
}
@InverseBindingAdapter(attribute = "android:text")
public static Double getDouble(TextView view) {
try {
return Double.parseDouble(view.getText().toString());
} catch (NumberFormatException e) {
return null;
}
}
)仅适用于基元,而不适用于像Double和Float这样的Object包装。
您可以使用BindingAdapters对包装器进行双向绑定。我写了这些,他们似乎工作正常:
<EditText
android:id="@+id/textView"
android:text="@={Converters.doubleToString(textView, item.dose)}" .../>
使用Android gradle插件3.0,您也可以使用转换功能,但除非您进行一些管理,否则输入会有点不稳定。你可以像这样绑定它:
@InverseMethod("doubleToString")
public static Double stringToDouble(TextView view, CharSequence value) {
if (value == null) {
return null;
}
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException e) {
return null;
}
}
public static CharSequence doubleToString(TextView view, Double value) {
if (value == null) {
return "";
}
String oldText = view.getText().toString();
if (!oldText.isEmpty()) {
try {
double oldVal = Double.parseDouble(oldText);
if (oldVal == value) {
return view.getText();
}
} catch (NumberFormatException e) {
// No problem. The TextView had text that wasn't formatted properly
}
}
return value.toString();
}
并有这样的转换器:
$('.expand').each(function(){
var reducedHeight = $(this).height();
$(this).css('height', 'auto');
var fullHeight = $(this).height();
$(this).height(reducedHeight);
$(this).data('reducedHeight', reducedHeight);
$(this).data('fullHeight', fullHeight);
}).click(function() {
$(this).animate({height: $(this).height() == $(this).data('reducedHeight') ? $(this).data('fullHeight') : $(this).data('reducedHeight')}, 500);
$(this).find('.expand_sign, .collapse_sign').toggleClass('hidden');
});
答案 1 :(得分:0)
你必须喜欢下面的内容
<android.support.v7.widget.AppCompatEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:hint="dosis"
android:inputType="numberDecimal"
android:minEms="5"
android:setText="@{workOrderFertilizer.dose}"/>
适配器中的
@BindingAdapter("setText")
public static void bindDoubleInText(AppCompatEditText tv, Object
d)
{
Double value = (Double) d;
if (tv.getText() == null) return;
// Get the actual EditText value
String edittextValue = tv.getText().toString();
if (edittextValue.isEmpty())
{
if (value != null) tv.setText(Double.toString(value));
}
else
{
if (value != null &&
Double.parseDouble(tv.getText().toString()) != value)
tv.setText(Double.toString(value));
}
}