2017-09-15 108 views
0

我在打开DatePicker而苦于打开EditText。它仅在第二次点击时打开日期选择器。我检查了以下选项,但没有任何工作。我已经在这里给我的代码使用XML请提供一个解决这个问题的方法。几乎我在为最多的尝试。在EditText中打开DatePicker首先点击不显示点击

<---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" /> 

而且我的Java代码是在这里:

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(); 

     } 
}); 
+1

我也有同样的问题。 –

+0

尽管切换到onTouchListener可以正常工作,但正确的方法是使用按钮,并将其设置为editText。 – lionscribe

回答

2

试试这个就会对上的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

+0

这个wll适合你吗? –

0

EditText不听OnClickListener,改用OnTouchListener,它会工作。

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

或者,如果你想使用OnClickListener,请使用以下方法:

<EditText 
    android:text="@+id/EditText01" 
    android:id="@+id/EditText01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusableInTouchMode="false" /> 
2

请使用setOnTouchListener代替setOnClickListener。 请使用如下代码:

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

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

         datePicker(); 

         break; 
       } 

       return false; 
      } 
     }); 

,并请删除此行的代码:

pDateEdt.setOnClickListener(this); 
0

我已经看到了很多程序员努力实现该功能为您正在尝试的。 现在你的场景中发生的事情是,当你第一次触摸Edittext时,edittext将进入焦点模式,并且当你第二次触摸相同的edittext时,只有onClick即datePicker()中的方法将被执行。 你应该做的就是删除android:focusableInTouchMode="false"标签 和地方这个标签的只是添加标签android:focusable="false" 并在Java代码中只是做以下那么

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

我希望你的代码将正常工作并会让您的第一次触摸时打开日期选取器。

相关问题