我的 EditText 仅由 SetText() 更新;每第二次尝试
My EditText is only updated by SetText(); every 2nd attempt
我正在尝试在 EditText 框中显示日期和时间,并在此处编写了一些代码。问题是,它正在工作 - 但仅限于第二次尝试。意思是我 select 日期和时间.. 没有任何反应,第二次尝试 setText();更新 EditText 以显示日期和时间。但是,您可能已经猜到了,我希望它立即更新 EditText。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reservation);
firebaseAuth = FirebaseAuth.getInstance();
textViewUserid = findViewById(R.id.addReservation_userId_edittext);
FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserid.setText(user.getEmail());
gestureDetector = new GestureDetector(this);
mDisplayDate = findViewById(R.id.addReservation_fromTime_edittext);
mDisplayDate2 = findViewById(R.id.addReservation_toTime_edittext);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
DatePickerDialog datePickerDialog = new DatePickerDialog(
AddReservationActivity.this,
android.R.style.Theme_Light,
mDateSetListener,
year,month,day);
TimePickerDialog timePickerDialog = new TimePickerDialog(
AddReservationActivity.this,
android.R.style.Theme_Light,
mTimeSetListener,
hour,minute,true);
timePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
timePickerDialog.show();
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
final String date = dayOfMonth + "/" + month + "/" + year;
mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (minute < 10) {
String time = "" + hourOfDay + ":" + "0"+minute;
mDisplayDate.getText().toString();
mDisplayDate.setText(date + " " + time);
}else {
String time = "" + hourOfDay + ":" + minute;
mDisplayDate.getText().toString();
mDisplayDate.setText(date + " " + time);
}
}
};
}
}
我已经尝试了几个小时来解决这个问题,遍历了 Whosebug 和许多其他站点。这有点令人沮丧,我真的希望有人对我做错了什么提出意见。
如果网站上已经有我错过的解决方案,我深表歉意。
非常感谢。
我找到了解决方案,按照评论中的建议附加了 EditText。因此,不要尝试同时使用日期和时间字符串设置 EditText 的文本。我将它们分开并制作了一个 setText(date);并附加时间:
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
final String date = dayOfMonth + "/" + month + "/" + year;
mDisplayDate.setText(date);
}
};
mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (minute < 10) {
String time = " " + hourOfDay + ":" + "0" + minute;
mDisplayDate.append(time);
} else {
String time = " " + hourOfDay + ":" + minute;
mDisplayDate.append(time);
}
}
};
我正在尝试在 EditText 框中显示日期和时间,并在此处编写了一些代码。问题是,它正在工作 - 但仅限于第二次尝试。意思是我 select 日期和时间.. 没有任何反应,第二次尝试 setText();更新 EditText 以显示日期和时间。但是,您可能已经猜到了,我希望它立即更新 EditText。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reservation);
firebaseAuth = FirebaseAuth.getInstance();
textViewUserid = findViewById(R.id.addReservation_userId_edittext);
FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserid.setText(user.getEmail());
gestureDetector = new GestureDetector(this);
mDisplayDate = findViewById(R.id.addReservation_fromTime_edittext);
mDisplayDate2 = findViewById(R.id.addReservation_toTime_edittext);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
DatePickerDialog datePickerDialog = new DatePickerDialog(
AddReservationActivity.this,
android.R.style.Theme_Light,
mDateSetListener,
year,month,day);
TimePickerDialog timePickerDialog = new TimePickerDialog(
AddReservationActivity.this,
android.R.style.Theme_Light,
mTimeSetListener,
hour,minute,true);
timePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
timePickerDialog.show();
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
final String date = dayOfMonth + "/" + month + "/" + year;
mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (minute < 10) {
String time = "" + hourOfDay + ":" + "0"+minute;
mDisplayDate.getText().toString();
mDisplayDate.setText(date + " " + time);
}else {
String time = "" + hourOfDay + ":" + minute;
mDisplayDate.getText().toString();
mDisplayDate.setText(date + " " + time);
}
}
};
}
}
我已经尝试了几个小时来解决这个问题,遍历了 Whosebug 和许多其他站点。这有点令人沮丧,我真的希望有人对我做错了什么提出意见。
如果网站上已经有我错过的解决方案,我深表歉意。 非常感谢。
我找到了解决方案,按照评论中的建议附加了 EditText。因此,不要尝试同时使用日期和时间字符串设置 EditText 的文本。我将它们分开并制作了一个 setText(date);并附加时间:
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
final String date = dayOfMonth + "/" + month + "/" + year;
mDisplayDate.setText(date);
}
};
mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (minute < 10) {
String time = " " + hourOfDay + ":" + "0" + minute;
mDisplayDate.append(time);
} else {
String time = " " + hourOfDay + ":" + minute;
mDisplayDate.append(time);
}
}
};