2015-11-19 31 views
0

我有一個關於EditText中的setOnClickListener事件的快速問題。我有一個EditText,它在觸摸時顯示一個對話框。但是當我調試時,我的應用程序在第一次觸摸時不顯示對話框,它只顯示在第二次觸摸中。我認爲問題是我使用TextTextLayout Edittext,但是當我嘗試一些解決方案時,這些不起作用。那麼如何在第一次接觸中展示我的對話?EditText中的Handler setOnClickListener事件

這裏是我的xml文件:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize" 
     android:orientation="vertical" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:paddingTop="60dp"> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_departure" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:completionThreshold="1" 
       android:id="@+id/actv_departure" 
       android:hint="Departure" 
       android:singleLine="true" 
       android:nextFocusDown="@+id/actv_arrival" 
       android:imeOptions="actionNext" 
       /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_arrival" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:completionThreshold="1" 
       android:id="@+id/actv_arrival" 
       android:hint="Arrival" 
       android:nextFocusDown="@+id/et_date" 
       android:imeOptions="actionNext" 
       android:singleLine="true" 
       /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_date" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/et_date" 
       android:hint="Date" 
       android:singleLine="true" /> 
     </android.support.design.widget.TextInputLayout> 

     <Button android:id="@+id/btn_searching" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Search" 
      android:background="@color/colorPrimary" 
      android:layout_marginTop="40dp" 
      android:textColor="@android:color/white"/> 

    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

這裏是MainActivity:

public class SearchFragment extends Fragment implements View.OnClickListener{ 
    private EditText et_date; 
    private TextInputLayout tilDate; 

    // private Button btn_searching; 
    private DatePickerDialog fromDatePickerDialog; 
    private SimpleDateFormat dateFormatter; 



    public SearchFragment(){ 

    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     et_date = (EditText)getView().findViewById(R.id.et_date); 
     et_date.setInputType(InputType.TYPE_NULL); 

     //et_date.requestFocus(); 
     tilDate= (TextInputLayout)getView().findViewById(R.id.input_layout_arrival); 
     dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); 
     setDateTimeField(); 

    } 

    private void setDateTimeField() { 
     try { 
      et_date.setOnClickListener(this); 
      tilDate.setOnClickListener(this); 
      final Calendar newCalendar = Calendar.getInstance(); 
      fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { 
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
        Calendar newDate = Calendar.getInstance(); 
        newDate.set(year, monthOfYear, dayOfMonth - 1); 
        if (newDate.getTime().getTime() > (newCalendar.getTime().getTime())) { 

         final AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show(); 

         //this code below is coppied in https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/ 
         final Timer t = new Timer(); 
         t.schedule(new TimerTask() { 
          public void run() { 
           builder.dismiss(); // when the task active then close the dialog 
           t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report 
          } 
         }, 2000); // after 2 second (or 2000 miliseconds), the task will be active. 
         et_date.setText(dateFormatter.format(newCalendar.getTime())); 

        } else { 
         newDate.set(year, monthOfYear, dayOfMonth); 
         et_date.setText(dateFormatter.format(newDate.getTime())); 
        } 
       } 
      }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 
     } catch (Exception ex) { 
      messages("Something Wrong!"); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fargment_search, container, false); 


     // Inflate the layout for this fragment 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    public void messages(String msg) { 
     new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage(msg).setNeutralButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }).show(); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v==et_date){ 
      fromDatePickerDialog.show(); 
     } 
    } 
+1

嘗試安卓採取

答:可調焦=「假」在你的EditText –

+0

@bhargav感謝你這麼多,它完美 –

回答

4

嘗試增加

android:focusable="false" 

它應該工作。

當小部件獲得焦點時,您的鍵盤彈出。所以爲了防止這種行爲將焦點設置爲false。從https://stackoverflow.com/a/12560605/4211264

+0

感謝你這麼多。 –