我剛剛進入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));
}
}
您是否嘗試在edittext上單擊偵聽器? – Pr38y
感謝您的repiy.I確實 – Hoo