gpt4 book ai didi

android - 如何在编辑 TextView 中显示当前日期

转载 作者:太空狗 更新时间:2023-10-29 16:19:44 35 4
gpt4 key购买 nike

这是我的代码,显示当前时间和日期我只想在编辑文本中显示当前日期,但同时显示日期和时间

这一行同时显示 lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));

                           public class MainActivity extends Activity {
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
EditText lblDateAndTime;
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
updateLabel();
}
};

private void updateLabel() {
lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
}

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lblDateAndTime = (EditText) findViewById(R.id.lblDateAndTime);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, d, myCalendar
.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
//
Button btnTime = (Button) findViewById(R.id.btnTime);
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(MainActivity.this, t, myCalendar
.get(Calendar.HOUR_OF_DAY), myCalendar
.get(Calendar.MINUTE), true).show();
}
});

updateLabel();
}// onCreate
} // class

最佳答案

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
editText.setText(dateFormat.format(new Date())); // it will show 16/07/2013
dateFormat = new SimpleDateFormat("dd MMM yyyy");
editText.setText(dateFormat.format(new Date())); // it will show 16 Jul 2013

关于android - 如何在编辑 TextView 中显示当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672150/

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