我的应用程序中有两个编辑文本,我想在他们的焦点上调用时间选择器,我成功地在编辑文本上调用时间选择器,但是想在编辑文本字段上添加不同的时间。 我有开始时间编辑文本和结束时间编辑文本。我提到了我的代码。
public class AccessWeekDays extends Activity
{
String message_selectedWeekDay;
TextView tvAdd;
TextView tvEdit;
TextView tvCurrentDay;
EditText edSub;
EditText edDesc;
EditText edStartTime;
EditText edEndTime;
TimePicker timepicksubject;
int phour;
int pminute;
Calendar calendar;
//listenner for start time
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
/*Date d=new Date();
String myformat="hh:mm:a";
SimpleDateFormat sdf = new SimpleDateFormat(myformat);
//String formated_time = sdf.format(calendar.getTime());
String formated_time = sdf.format(d);*/
phour = hourOfDay;
pminute = minute;
updateDisplaystart();
}
private void updateDisplaystart()
{
edStartTime.setText(new StringBuilder()
.append(pad(phour)).append(":")
.append(pad(pminute)));
}
private String pad(int c)
{
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.adding_timtable_part);
calendar=Calendar.getInstance();
tvAdd=(TextView)findViewById(R.id.textAdd);
tvEdit=(TextView)findViewById(R.id.textEdit);
tvCurrentDay=(TextView)findViewById(R.id.textCurentDay);
edSub=(EditText)findViewById(R.id.edSubjectname);
edDesc=(EditText)findViewById(R.id.edDesc);
edStartTime=(EditText)findViewById(R.id.edStartTime);
edEndTime=(EditText)findViewById(R.id.edEndTime);
timepicksubject=(TimePicker)findViewById(R.id.timePickersubject);
edSub.setVisibility(View.GONE);
edDesc.setVisibility(View.GONE);
edStartTime.setVisibility(View.GONE);
edEndTime.setVisibility(View.GONE);
Intent intent = getIntent();
message_selectedWeekDay=intent.getExtras().getString("selectedweekdays");
setCurrentDay();
tvAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvAdd.setText("Done");
tvEdit.setText("Cancel");
edSub.setVisibility(View.VISIBLE);
edDesc.setVisibility(View.VISIBLE);
edStartTime.setVisibility(View.VISIBLE);
edEndTime.setVisibility(View.VISIBLE);
}
});
edStartTime.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(1);
}
});
edEndTime.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(1);
}
});
}//oncreate ends
@Override
@Deprecated
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 1:
return new TimePickerDialog(this, mTimeSetListener, phour, pminute,false);
// return new TimePickerDialog(context, callBack, hourOfDay, minute, is24HourView)
default:
break;
}
return null;
}
我正在调用 updateDisplaystart()方法,该方法会写入编辑文本的值并告诉我如何更改结束时间编辑文本值。
答案 0 :(得分:0)
我建议你这样:
在EditText
方法中传递showDialog
并使用该方法更改EditText
值。
示例:
//your focus listener snippet
edStartTime.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(1, edStartTime);
}
});
edEndTime.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(1, edEndTime);
}
});
//showDialog method should be changed to handle second argument of EditText
public void showDialog(int id, EditText edText){
//pass the EditText along with other arguments
}
现在,当onTimeSet
被调用时,更改该EditText中的文本。