Double-click Date Picker Dialog Box

I have a search activity with two edit text fields. I like the date picker dialog box in the text edit dialog box. However, when I click on the text editing, the keyboard is displayed first, then after the second click, the date selection dialog opens. Can someone help me?

Here is the activity code

package com.example.firstdemoapp.activities; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import com.example.firstdemoapp.R; import com.example.firstdemoapp.model.StatusDK; import android.app.Activity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; public class SearchingTaxActivity extends Activity implements OnClickListener, DatePickerDialog.OnDateSetListener, OnItemSelectedListener { private Calendar calendarFrom; private Calendar calendarTo; private String myFormat; private SimpleDateFormat sdf; private EditText dateFrom; private EditText dateTo; private EditText activeEditText; private Calendar activeCalendar; private Spinner spinnerStatusDK; private ArrayAdapter spinnerArrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_tax); calendarFrom = Calendar.getInstance(); calendarTo = Calendar.getInstance(); myFormat="dd/MM/yyyy"; sdf = new SimpleDateFormat(myFormat, Locale.US); dateFrom = (EditText) findViewById(R.id.dateFrom); dateTo = (EditText) findViewById(R.id.dateTo); spinnerStatusDK=(Spinner)findViewById(R.id.spinnerStatusDK); spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, new StatusDK[] { new StatusDK( 0, "0" ), new StatusDK( 1, "1" ), new StatusDK( 2, "2" ), }); spinnerStatusDK.setAdapter(spinnerArrayAdapter); spinnerStatusDK.setOnItemSelectedListener(this); dateFrom.setOnClickListener(this); dateTo.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == dateFrom) { activeCalendar = calendarFrom; activeEditText = dateFrom; } else if (v == dateTo) { activeCalendar = calendarTo; activeEditText = dateTo; } new DatePickerDialog(SearchingTaxActivity.this, this, activeCalendar.get(Calendar.YEAR), activeCalendar.get(Calendar.MONTH), activeCalendar.get(Calendar.DAY_OF_MONTH)).show(); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub activeCalendar.set(Calendar.YEAR, year); activeCalendar.set(Calendar.MONTH, monthOfYear); activeCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); if (activeEditText != null) { activeEditText.setText(sdf.format(activeCalendar.getTime())); } } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } 

}

and layout for this operation:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/dateFromTextView" /> <EditText android:id="@+id/dateFrom" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/edit_datefrom" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/dateToTextView" /> <EditText android:id="@+id/dateTo" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/edit_dateto" /> <Spinner android:id="@+id/spinnerStatusDK" android:layout_width="match_parent" android:layout_height="wrap_content" /> 

+6
source share
3 answers

I added android: focusableInTouchMode = "false" to edit the text in the XML file, and that helped me. Because the first focal event is fired, then click the event that will be fired.

+30
source

you can hide the keyboard using the InputMethodManager class

 private void hideKeyboard(EditText et) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(et.getWindowToken(), 0); } 
0
source

This is the best solution, which is very useful for opening any DatePicker and TimePicker from EditText ,

 editdate.setFocusable(false); editdate.setKeyListener(null); 
0
source

Source: https://habr.com/ru/post/974593/


All Articles