MaterialTimePicker 片段未显示
MaterialTimePicker fragment not showing up
android 开发新手。我想使用 MaterialTimePicker 从用户那里获取时间输入,并在 TextField
中显示时间。使用 Button
上的 onClickListener
激活 MaterialTimePicker 片段。我也在 java
中开发,我不确定 material.io 网站上的文档是针对 java 还是 kotlin。
btnobj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timepicker();
}
});
void timepicker(){
MaterialTimePicker picker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setTitleText("Select Start Time")
.setHour(12)
.setMinute(10)
.build();
picker.show(getSupportFragmentManager(), "starttime");
txtfieldobj.setText(String.valueOf(picker.getHour())+" : "+String.valueOf(picker.getMinute()));
}
错误:目前,当我 运行 此代码在我单击按钮后出现一秒钟的空白页面,然后它返回到父级 activity。
我试过你的例子,你的问题是你需要删除这一行
txtfieldobj.setText(String.valueOf(picker.getHour())+" : "+String.valueOf(picker.getMinute()));
因为它使应用崩溃
如果你想在点击确定后显示日期和时间,这里是代码:
private final SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());
void timepicker() {
MaterialTimePicker picker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setTitleText("Select Start Time")
.setHour(12)
.setMinute(10)
.build();
picker.addOnPositiveButtonClickListener(dialog -> {
int newHour = picker.getHour();
int newMinute = picker.getMinute();
DashBoardActivity.this.onTimeSet(newHour, newMinute);
});
picker.show(getSupportFragmentManager(), "starttime");
}
private void onTimeSet(int newHour, int newMinute) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, newHour);
cal.set(Calendar.MINUTE, newMinute);
cal.setLenient(false);
String format = formatter.format(cal.getTime());
txtfieldobj.setText(format);
}
android 开发新手。我想使用 MaterialTimePicker 从用户那里获取时间输入,并在 TextField
中显示时间。使用 Button
上的 onClickListener
激活 MaterialTimePicker 片段。我也在 java
中开发,我不确定 material.io 网站上的文档是针对 java 还是 kotlin。
btnobj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timepicker();
}
});
void timepicker(){
MaterialTimePicker picker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setTitleText("Select Start Time")
.setHour(12)
.setMinute(10)
.build();
picker.show(getSupportFragmentManager(), "starttime");
txtfieldobj.setText(String.valueOf(picker.getHour())+" : "+String.valueOf(picker.getMinute()));
}
错误:目前,当我 运行 此代码在我单击按钮后出现一秒钟的空白页面,然后它返回到父级 activity。
我试过你的例子,你的问题是你需要删除这一行
txtfieldobj.setText(String.valueOf(picker.getHour())+" : "+String.valueOf(picker.getMinute()));
因为它使应用崩溃
如果你想在点击确定后显示日期和时间,这里是代码:
private final SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());
void timepicker() {
MaterialTimePicker picker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setTitleText("Select Start Time")
.setHour(12)
.setMinute(10)
.build();
picker.addOnPositiveButtonClickListener(dialog -> {
int newHour = picker.getHour();
int newMinute = picker.getMinute();
DashBoardActivity.this.onTimeSet(newHour, newMinute);
});
picker.show(getSupportFragmentManager(), "starttime");
}
private void onTimeSet(int newHour, int newMinute) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, newHour);
cal.set(Calendar.MINUTE, newMinute);
cal.setLenient(false);
String format = formatter.format(cal.getTime());
txtfieldobj.setText(format);
}