代码之家  ›  专栏  ›  技术社区  ›  M.ArslanKhan

如何使用数据绑定在按钮单击后显示和隐藏视图

  •  0
  • M.ArslanKhan  · 技术社区  · 5 年前

    我有两个编辑字段,一个提交按钮和一个文本视图,首先用户填写编辑字段,然后单击提交按钮。我必须检查所有字段是否有效,如果有效,那么我想显示使用数据绑定的显示文本视图。 我有一个XML数据绑定问题。我想我错过了什么 这是我的文本视图

    <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;
        }
    

    }

    0 回复  |  直到 5 年前
        1
  •  0
  •   edafe    5 年前

    这可能不是你能找到的最好的答案,但它确实能完成工作。

    因为我们没有使用双向数据绑定,所以我在mainactivity中监听了submit按钮的一次单击,在这里我更改了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}"
    
        2
  •  0
  •   Nhật Trần    5 年前

    您应该在MainActivity中这样做,因为hide或show textview是updateUI,所以您不应该在otpnotebinderhelper类中这样做。