MVVM:如何连接模型 class 中的字符串?
MVVM : How to Concat the String in model class?
我使用改装回调创建了应用程序。在那里我想用文字显示一些信息。在 textView 中我已经绑定了数据,我还需要连接一些文本。
我的代码如下
查看:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="UserProfile"
type="com.practical_session.sai.instaprouser.Profile.model.UserProfileInfo" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear"
android:orientation="vertical"
android:background="@drawable/animation_list"
tools:context="com.practical_session.sai.instaprouser.Profile.view.ProfileActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:textSize="20dp"
android:text="@{UserProfile.username}"
android:textColor="@android:color/black" />
</RelativeLayout>
</LinearLayout>
</layout>
型号:
public class UserProfileInfo extends BaseObservable {
@SerializedName("username")
@Expose
private String username;
@Bindable
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
预期输出:
型号Return + "Concat String"
将其连接到 xml
android:text="@{UserProfile.username + `Concat String`}"
试试这个简单的方法
您所有的处理事物和业务逻辑都应该在控制器中 Class(即:MVVM 中的 ViewModel)
在Controller中写一个方法,把值传给Controller,然后那个方法returns用Concat String
传值
代码示例
android:text="@{Controller.getUserProfile(UserProfile.username)}"
控制器方法
public String getUserProfile(String name)
{
return name + "concat string";
}
我使用改装回调创建了应用程序。在那里我想用文字显示一些信息。在 textView 中我已经绑定了数据,我还需要连接一些文本。
我的代码如下
查看:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="UserProfile"
type="com.practical_session.sai.instaprouser.Profile.model.UserProfileInfo" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear"
android:orientation="vertical"
android:background="@drawable/animation_list"
tools:context="com.practical_session.sai.instaprouser.Profile.view.ProfileActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:textSize="20dp"
android:text="@{UserProfile.username}"
android:textColor="@android:color/black" />
</RelativeLayout>
</LinearLayout>
</layout>
型号:
public class UserProfileInfo extends BaseObservable {
@SerializedName("username")
@Expose
private String username;
@Bindable
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
预期输出:
型号Return + "Concat String"
将其连接到 xml
android:text="@{UserProfile.username + `Concat String`}"
试试这个简单的方法
您所有的处理事物和业务逻辑都应该在控制器中 Class(即:MVVM 中的 ViewModel)
在Controller中写一个方法,把值传给Controller,然后那个方法returns用Concat String
传值代码示例
android:text="@{Controller.getUserProfile(UserProfile.username)}"
控制器方法
public String getUserProfile(String name)
{
return name + "concat string";
}