Android 使用set不';使用数据绑定不会影响视图
我试图使用数据绑定更改不同的EditText属性,但它似乎不起作用 自定义_edittext.xml:Android 使用set不';使用数据绑定不会影响视图,android,android-layout,android-databinding,Android,Android Layout,Android Databinding,我试图使用数据绑定更改不同的EditText属性,但它似乎不起作用 自定义_edittext.xml: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="textVar" ty
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="textVar"
type="String"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/descriptionTextEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ems="12"
android:inputType="textMultiLine"
android:text="@{textVar}"/>
</RelativeLayout>
</layout>
我的名字是:
private void init(Context context, AttributeSet attrs){
View.inflate(context, R.layout.custom_edittext, this);
CustomEdittextBinding stomEdittextBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_edittext, null, false);
Drawable drawableButton = context.getResources().getDrawable(R.drawable.ic_action_name);//context.getResources().getDrawable(R.drawable.ic_action_name);
customEdittextBinding.setTextVar("new text"); //it should change the text, shouldn't it?
}
我的主要活动:
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
在activity_main.xml中,我像这样添加了CustomEditText,但有一个错误——找不到以下类
<com.example.xxx.app.CustomEditText
android:id="@+id/custom"
android:layout_width="344dp"
android:layout_height="50dp"
android:paddingLeft="100dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >
好的,我通过在实例化customEdittextBinding后添加
addView(customEdittextBinding.getRoot());
解决了这个问题
customEditText = findViewById(R.id.custom);
}
<com.example.xxx.app.CustomEditText
android:id="@+id/custom"
android:layout_width="344dp"
android:layout_height="50dp"
android:paddingLeft="100dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >