当 textField 获得焦点时显示 TimePicker
Display TimePicker when textField is focused
我刚刚进入 android 应用程序,现在尝试使用允许用户在文本框中输入时间的界面。当文本框获得焦点时,将显示时间选择器对话框。但是现在当我点击文本框时,日期选择器对话框不显示。
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));
}
}
尝试在 xml 文件中设置 android:clickable="true" 和 android:focusableInTouchMode="false"。
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));
}
}
我刚刚进入 android 应用程序,现在尝试使用允许用户在文本框中输入时间的界面。当文本框获得焦点时,将显示时间选择器对话框。但是现在当我点击文本框时,日期选择器对话框不显示。
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));
}
}
尝试在 xml 文件中设置 android:clickable="true" 和 android:focusableInTouchMode="false"。
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));
}
}