gpt4 book ai didi

android - 将 Id 从 Button 传递到 timePicker 对话框

转载 作者:行者123 更新时间:2023-11-30 04:12:10 25 4
gpt4 key购买 nike

我有一个 timePicker 对话框,可以从不同的按钮调用它。如何传递调用对话框的 Id?

这是相关代码:

    //Time picker
private int pHour;
private int pMinute;
static final int TIME_DIALOG_ID = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/** Get the current time */
final Calendar cal = Calendar.getInstance();
pHour = cal.get(Calendar.HOUR_OF_DAY);
pMinute = cal.get(Calendar.MINUTE);
}

按钮的点击事件:

public void onClick(View v) {

// Here I should pass the v.getId() to the dialog
showDialog(TIME_DIALOG_ID);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, pHour, pMinute, false);
}
return null;
}

/** Callback received when the user "picks" a time in the dialog */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
pHour = hourOfDay;
pMinute = minute;
//Here I should know from which v.getId() the dialog was called
}
};

谢谢!

最佳答案

你必须为你想做的事情写你自己的选择器:

假设您必须为所有按钮调用同一个选择器,我建议您在 Button 上使用 switch 语句

 public void onClick(View v) {

switch(v.getId()){

case R.id.Button1:
showDialog(TIME_DIALOG_ID);
break;

case R.id.Button2:
showDialog(TIME_DIALOG_ID);
break;
}
}

如果您希望弹出不同的Dialog,您可以使用不同的Dialog ID。

关于android - 将 Id 从 Button 传递到 timePicker 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673675/

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