gpt4 book ai didi

java - 从 TextView 文本设置 TimePicker 时间

转载 作者:行者123 更新时间:2023-12-02 02:02:49 25 4
gpt4 key购买 nike

我用了this post从 TextView 创建 TimePicker。现在,我想显示选择了前一个时间的 TimePicker。

所以我第一次打开 TimePicker 并设置为 08:15。第二次,我想在 TimePicker 上显示预设时间 08:15,而不是当前的小时:分钟。

我想我需要更改这部分代码:

   @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

最佳答案

试试这个代码..

public class displayTime extends AppCompatActivity {
TextView textView;
int hour,min;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.amTvData);
Calendar mcurrentTime = Calendar.getInstance();
hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
min = mcurrentTime.get(Calendar.MINUTE);

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showTime(hour,min);
}
});
}
void showTime(int hours, int minte){
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(displayTime.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
textView.setText( selectedHour + ":" + selectedMinute);
hour=selectedHour;
min=selectedMinute;
}
}, hours, minte, false);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();

}
}

关于java - 从 TextView 文本设置 TimePicker 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51304801/

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