2017-09-15 107 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(); 
} 

我希望你的代碼將正常工作並會讓您的第一次觸摸時打開日期選取器。

相關問題