我有2个Editfields,一个提交按钮和一个Textview,首先用户填写了编辑字段,然后单击提交按钮。我必须检查所有字段是否有效,如果有效,那么我想显示使用数据绑定显示textview。我在xml中遇到了数据绑定问题。我想我错过了什么这是我的textview
<data>
variable
name="helper"
type="com.abc.online.views.helper.OtpNoteBinderHelper" />
</data>
<TextView
android:id="@+id/idtverrorMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_3sdp"
android:fontFamily="@font/montserrat_regular"
android:gravity="center_horizontal"
android:letterSpacing="-0.01"
android:text="@string/ops_looks_like_you_apos_ve_entered_a_wrong_code"
android:textColor="#ff3b30"
android:textSize="@dimen/text_size_9"
app:presentation_note="false"
android:visibility="@{helper.is_presentation_note_visible ? View.VISIBLE : View.GONE}" />
这是我的助手课程
public class OtpNoteBinderHelper extends BaseObservable {
private Boolean presentation_note;
private Boolean timerElementsVisible;
public OtpNoteBinderHelper(){
this.presentation_note = true;
this.timerElementsVisible = false;
}
public void setPresentationNoteViewVisible(boolean presentationElementsVisible) {
this.presentation_note = presentationElementsVisible;
}
@Bindable
public Boolean getPresentation_note() {
return presentation_note;
}
}
这可能不是你会发现的最好的答案,但它确实完成了工作..
由于我们没有使用双向数据绑定,我在MainActivity中听取了提交按钮的onClick,我在其中更改了NoteBinderhelper变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
final NoteBinderhelper noteBinderhelper = new NoteBinderhelper();
mainBinding.setHelper(noteBinderhelper);
mainBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String textOne = mainBinding.editText1.getText().toString();
String textTwo = mainBinding.editText2.getText().toString();
if(textOne.isEmpty() || textTwo.isEmpty()){
noteBinderhelper.setPresentationNoteViewVisible(false);
} else {
noteBinderhelper.setPresentationNoteViewVisible(true);
}
mainBinding.setHelper(noteBinderhelper);
}
});
}
在NoteBinderHelper类中稍作修改
public class NoteBinderhelper extends BaseObservable {
private Boolean presentation_note;
private Boolean timerElementsVisible;
public NoteBinderhelper(){
this.presentation_note = true;
this.timerElementsVisible = false;
}
public void setPresentationNoteViewVisible(boolean presentationElementsVisible) {
this.presentation_note = presentationElementsVisible;
notifyPropertyChanged(BR.presentation_note);
}
@Bindable
public Boolean getPresentation_note() {
return presentation_note;
}
}
我将textView的可见性更改为此
android:visibility="@{helper.presentation_note ? View.VISIBLE : View.GONE}"
你应该在你的MainActivity中做,因为隐藏或显示textView是updateUI,所以你不要在OtpNoteBinderHelper类中做