2015-09-18 64 views
0

我剛剛進入android應用程序,現在嘗試使用允許用戶在文本框中輸入時間的界面。時間選擇器對話框將在文本框被關注時顯示。但是現在,當我單擊文本框時,日期選取器對話框不顯示。當textField聚焦時顯示TimePicker

WorkDetails.java

public void onFocusChange(View v, boolean hasFocus) { 
     EditText txtTime = (EditText) findViewById(R.id.editText6); 
     txtTime.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus) 
       { 
        TimePick time = new TimePick(v); 
        FragmentTransaction ft = getFragmentManager().beginTransaction(); 
        time.show(ft, "TimePicker"); 

       } 
      } 
     }); 
    } 

TimePick.java

@SuppressLint("ValidFragment") 
public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 
    private TextView time; 

    public TimePick(View view) 
    { 
     time=(EditText)view; 
    } 


    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     final Calendar c= Calendar.getInstance(); 
     int hour=c.get(Calendar.HOUR_OF_DAY); 
     int minute=c.get(Calendar.MINUTE); 

     //Toast.makeText(getActivity(),this,"Date: "+year+"-",+month+"-"+day,Toast.LENGTH_SHORT).show(); 
     return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view,int hourofDay, int minute) 
    { 

     time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute)); 



    } 


} 
+2

您是否嘗試在edittext上單擊偵聽器? – Pr38y

+0

感謝您的repiy.I確實 – Hoo

回答

0
editText = (EditText)findViewById(R.id.editText); 
    editText2 = (EditText)findViewById(R.id.editText2); 
    editText3 = (EditText)findViewById(R.id.editText3); 

    editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() 
    { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) 
     { 
      String input; 
      EditText editText; 

      if(!hasFocus) 
      { 
       TimePick time = new TimePick(v); 
       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       time.show(ft, "TimePicker"); 
      } 
     } 
    }); 


    editText2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      TimePick time = new TimePick(v); 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      time.show(ft, "TimePicker"); 
     } 
    }); 
} 

public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener 
{ 
    private TextView time; 

    public TimePick(View view) 
    { 
     time=(EditText)view; 
    } 

    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     final Calendar c= Calendar.getInstance(); 
     int hour=c.get(Calendar.HOUR_OF_DAY); 
     int minute=c.get(Calendar.MINUTE); 
     //Toast.makeText(getActivity(),this,"Date: "+year+"-",+month+"-"+day,Toast.LENGTH_SHORT).show(); 
     return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity())); 
    } 
    public void onTimeSet(TimePicker view,int hourofDay, int minute) 
    { 
     time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute)); 
    } 
} 
0

嘗試設置機器人:可點擊= 「真」 和android:focusableInTouchMode = xml文件中 「假」。

+0

嘗試過,但不工作 – Hoo