如何在浮动标签更改TextInputLayout中的状态时执行操作?

时间:2016-10-12 12:01:42

标签: android calendar onclicklistener android-textinputlayout

我在TextInputLayout中有一个Edittext。我想这样做,当我点击这个textinputlayout然后日历也打开。但在我的情况下浮动标签改变状态,当我点击edittext然后日历打开。但我不想那样做我想要当用户第一次点击TextInputLayout时浮动标签更改并且日历也随之打开。任何人都告诉我该怎么做?

这是我的xml: -

<android.support.design.widget.TextInputLayout
                    android:id="@+id/til_dob"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/_10sdp"
                    android:clickable="true"
                    android:textColorHint="@color/colorBlack">

                    <EditText
                        android:id="@+id/DateOfBirth"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/date"
                        android:inputType="none"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:textSize="15sp" />
                </android.support.design.widget.TextInputLayout>

这是我在edittext上的点击列表器: -

 @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.DateOfBirth:

                new DatePickerDialog(getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();

                break;

         }
    }

1 个答案:

答案 0 :(得分:0)

第一次单击TextInputLayout时,应该使用OnFocusChangeListener作出反应(这意味着TextInputLayout内的EditText获得焦点)

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  @Override public void onFocusChange(View v, boolean hasFocus) {
    //Open the calendar
    new DatePickerDialog(getContext(), date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
  }
});

&#34; lambda版本&#34;将是:

editText.setOnFocusChangeListener((v, hasFocus) -> 
   new DatePickerDialog(getContext(), date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show());