Opening DatePicker in EditText click not showing at first click

java android datepicker android-edittext

1991 观看

6回复

51 作者的声誉

I'm struggling with opening a DatePicker while click on a EditText . It opens the date picker at the second click only. I have checked with the following options but nothing worked. I have given my code with XML here Kindly suggest me a solution for this problem. Almost i'm trying for the most.

<---XML content--->
<EditText
      android:id="@+id/p_date_edt"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"                                
      android:layout_marginBottom="@dimen/margin_10dp"
      android:ems="8"
      android:hint="@string/dateTime"
      android:focusableInTouchMode="false" />

And my java code is here:

pDateEdt = (EditText) view.findViewById(R.id.p_date_edt);
pDateEdt.setFocusable(false);
pDateEdt.setOnClickListener(this);
      ----

pDateEdt.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           datePicker();

       }
});
作者: Viswa Sundhar 的来源 发布者: 2017 年 9 月 15 日

回应 6


0

1936 作者的声誉

EditText does not listen to OnClickListener, instead use OnTouchListener, it will work.

pDateEdt.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {                
        datePicker();
        return false;
    }
});

Or, if you want to use OnClickListener, use the following method:

<EditText
    android:text="@+id/EditText01"
    android:id="@+id/EditText01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false" />
作者: Raja 发布者: 2017 年 9 月 15 日

3

1169 作者的声誉

Please use setOnTouchListener instead of setOnClickListener. Please use this below code :

pDateEdt.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:

                         datePicker();

                        break;
                }

                return false;
            }
        });

and please Remove this line of code :

pDateEdt.setOnClickListener(this);
作者: chandrakant sharma 发布者: 2017 年 9 月 15 日

0

315 作者的声誉

I have seen a lot of programmers struggling to achieve this feature as your are trying. Now in your scenario what is happening is when you touch Edittext for the first time, the edittext will get into focus mode, and when you touch the same edittext for second time then only the method in the onClick i.e. datePicker() will execute. So what you are supposed to do is just remove the android:focusableInTouchMode="false" tag and in place of this tag just add the tag android:focusable="false" and in your java code just do the following

pDateEdt = (EditText) view.findViewById(R.id.p_date_edt);
pDateEdt.setOnClickListener(this);
@Override
public void onClick(View v) {
    datePicker();
}

i hope your code will work fine and will let the Date Picker open on your first touch.

作者: Pranay Soni 发布者: 2017 年 9 月 15 日

3

1576 作者的声誉

决定

Try this it will works on single click on edittext

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DatePickerDialog mDatePickerDialog;
    private EditText edDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edDate = (EditText) findViewById(R.id.activity_ed_date);

        setDateTimeField();
        edDate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mDatePickerDialog.show();
                return false;
            }
        });
    }

    private void setDateTimeField() {

        Calendar newCalendar = Calendar.getInstance();
        mDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy");
                final Date startDate = newDate.getTime();
                String fdate = sd.format(startDate);

                edDate.setText(fdate);

            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

    }
}

activity_main.xml

<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.demoapp.MainActivity">

    <EditText
        android:id="@+id/activity_ed_date"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:focusable="false"
        android:hint="Date"
        android:imeOptions="actionNext"
        android:maxLength="30" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary" />

</android.support.constraint.ConstraintLayout>

enter image description here

作者: Shubham Sejpal 发布者: 2017 年 9 月 15 日

0

717 作者的声誉

Its the most simplest way to work with date picker. Just add two variable

private DatePickerDialog datePickerDialog;
EditText etJoiningDate;

etJoiningDate=(EditText)findViewById(R.id.etJoiningDate);

    etJoiningDate.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:

                    final Calendar cldr = Calendar.getInstance();
                    int day = cldr.get(Calendar.DAY_OF_MONTH);
                    int month = cldr.get(Calendar.MONTH);
                    int year = cldr.get(Calendar.YEAR);
                    // date picker dialog
                    datePickerDialog = new DatePickerDialog(TestActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                    etJoiningDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                                }
                            }, year, month, day);
                    datePickerDialog.show();

                    break;
            }


            return false;
        }


    });
作者: Hasan Masud 发布者: 2018 年 5 月 3 日

0

1 作者的声誉

Probably the Best and easiest way to solve this problem:

In the layout file, set these attributes for the EditText

android:clickable="true"
android:longClickable="false"
android:focusable="false"
作者: Teja Karlapudi 发布者: 2018 年 11 月 29 日
32x32