gpt4 book ai didi

java - 时间选择器对话框

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:03 25 4
gpt4 key购买 nike

我正在使用多个带有 onclick 监听器的 TextView ,这些监听器会将我定向到 TimePicker 对话框。

但是看完教程后,我似乎不知道如何编写代码,

    textview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});

textview2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});

显示对话框:

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, true);
}
return null;
}

最后,

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
}
};

现在,对于在TimePickerDialog.OnTimeSetListener回调后调用时间选择器的textview的.setText,我应该如何编写代码?

答案很简单,但我缺乏 OOP 知识并且仍在学习中。谢谢...

最佳答案

您需要使用全局变量。比如说,String fromWhere="";

现在,

    textview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fromWhere="textview1";
showDialog(TIME_DIALOG_ID);
}
});

textview2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fromWhere="textview2";
showDialog(TIME_DIALOG_ID);
}
});
...
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {

if(fromWhere.equals(textview1))
// set hour and minute to according variable and textview
else if(fromWhere.equals(textview2))
// set hour and minute to according variable and textview
.. //so on for others
//mHour = hourOfDay;
//mMinute = minute;
fromWhere="";
}
};

关于java - 时间选择器对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383550/

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