Android DataBinding在哪里获取上下文?

时间:2022-09-01 14:31:07

I have TextView for showing time. I want to use Android's DataBinding plugin. For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it possible to get context include element? Or do I have to use old school way?

我有TextView显示时间。我想使用Android的DataBinding插件。对于格式化时间,我使用DateUtils.formatDateTime(context,int,int)方法,该方法接受Context实例。是否有可能获得上下文包含元素?或者我必须使用旧学校的方式?

Thanks

3 个解决方案

#1


24  

Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method. Alternatively, you can create a custom attribute for data binding:

以为我应该回答而不是发表评论。当rc2发布时,你会有更多的选择。在rc1中,您可以将变量中的上下文传递给Binding,然后将其作为参数传递给方法。或者,您可以为数据绑定创建自定义属性:

@BindingAdapter({"timeMillis", "dateFlags"})
public static void setDateText(TextView view, int timeMillis, int dateFlags) {
    view.setText(DateUtils.formatDateTime(view.getContext(), timeMillis,
                 dateFlags));
}

And then use it in your TextView:

然后在TextView中使用它:

<TextView ... app:timeMillis="@{timeVar}" app:dateFlags="@{dateFlags}"/>

#2


38  

Also you can do something like this in your view using the current view context as parameter.

您也可以在视图中使用当前视图上下文作为参数执行类似的操作。

...
android:text="@{yourModelHere.yourModelMethodHere(context)}"
...

#3


2  

A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

根据需要生成名为context的特殊变量以用于绑定表达式。 context的值是来自根View的getContext()的Context。上下文变量将被具有该名称的显式变量声明覆盖。

In other words, every time you need to pass the context just use "context" as in @{Object.method(context)}.

换句话说,每次需要传递上下文时,只需使用@ {Object.method(context)}中的“context”。

#1


24  

Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method. Alternatively, you can create a custom attribute for data binding:

以为我应该回答而不是发表评论。当rc2发布时,你会有更多的选择。在rc1中,您可以将变量中的上下文传递给Binding,然后将其作为参数传递给方法。或者,您可以为数据绑定创建自定义属性:

@BindingAdapter({"timeMillis", "dateFlags"})
public static void setDateText(TextView view, int timeMillis, int dateFlags) {
    view.setText(DateUtils.formatDateTime(view.getContext(), timeMillis,
                 dateFlags));
}

And then use it in your TextView:

然后在TextView中使用它:

<TextView ... app:timeMillis="@{timeVar}" app:dateFlags="@{dateFlags}"/>

#2


38  

Also you can do something like this in your view using the current view context as parameter.

您也可以在视图中使用当前视图上下文作为参数执行类似的操作。

...
android:text="@{yourModelHere.yourModelMethodHere(context)}"
...

#3


2  

A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

根据需要生成名为context的特殊变量以用于绑定表达式。 context的值是来自根View的getContext()的Context。上下文变量将被具有该名称的显式变量声明覆盖。

In other words, every time you need to pass the context just use "context" as in @{Object.method(context)}.

换句话说,每次需要传递上下文时,只需使用@ {Object.method(context)}中的“context”。