gpt4 book ai didi

java - 我的EditText仅由SetText()更新;每第二次尝试

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:58 26 4
gpt4 key购买 nike

我正在尝试在 EditText 框中显示日期和时间,并在此处编写了一些代码。问题是,它有效 - 但仅在第二次尝试时有效。意思是我选择日期和时间..没有任何反应,第二次尝试 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);

}
}
};
}
}

我已经尝试了几个小时来解决这个问题,浏览了 Stackoverflow 和许多其他网站。这有点令人沮丧,我真的希望有人对我做错的事情提出意见。

如果我错过的网站上已经有解决方案,我深表歉意。预先非常感谢。

最佳答案

我找到了解决方案,附加了 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);

}
}
};

关于java - 我的EditText仅由SetText()更新;每第二次尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53050762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com