gpt4 book ai didi

android - 带有中性按钮的 TimePickerDialog

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:13 27 4
gpt4 key购买 nike

我有一个带有“TimePreference”类的 TimePickerDialog 首选项,它扩展了 DialogPreference:

public class TimePreference extends DialogPreference{
...
private TimePicker picker=null;

...
public TimePreference(Context ctxt, AttributeSet attrs){
super(ctxt, attrs);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}

@Override
protected View onCreateDialogView(){
picker=new TimePicker(getContext());
return(picker);
}
...
}

我想添加第三个按钮,它将首选项设置为当前时间并关闭对话框,如下所示:

enter image description here

问题是,是否有某种方法可以使用 TimePickers 执行此操作,还是我应该制作自定义 AlertDialog?

最佳答案

感谢您的回答!我将向您展示我最终是如何做到的,以防万一有人想做同样的事情。

首先,我有一个布局文件 good_timedialog.xml,它看起来像我想要的: TimePicker with Positive, neutral and negative buttons

然后我有一个 TimePreference 类:

public class TimePreference extends DialogPreference{
private final static String lTag="CustomTimeDialog";
private TimePicker picker;
protected TimeObj lastTime;

public TimePreference(Context ctxt,AttributeSet atrs) {
super(ctxt,atrs);
setDialogLayoutResource(R.layout.good_timedialog); // this loads the layout
setPositiveButtonText("");
setNegativeButtonText(""); // hide default buttons
setDialogTitle(ctxt.getString(R.string.selectTimeTimePrefTitle));
}

@Override
protected void onBindDialogView(View v){
super.onBindDialogView(v);

v.findViewById(R.id.butCancel).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Log.d(lTag,"cancel clicked");
getDialog().dismiss();
}
});
v.findViewById(R.id.butNow).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Log.d(lTag,"now clicked");
saveToSP(true);
getDialog().dismiss();
}
});
v.findViewById(R.id.butOK).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Log.d(lTag,"ok clicked");
saveToSP(false);
getDialog().dismiss();
}
});

picker = (TimePicker) v.findViewById(R.id.timePicker);
if (picker!=null) {
picker.setCurrentHour(lastTime.hr);
picker.setCurrentMinute(lastTime.min);
} else { // if it is null
Log.w(lTag,"var picker is null!");
}

}

void saveToSP(boolean now){
if (now) lastTime = new TimeObj(); // make it the current time
else {
lastTime.hr = picker.getCurrentHour();
lastTime.min = picker.getCurrentMinute();
}
setSummary(lastTime.giveString()); // update Summary like hh:mm 16:28

int time = lastTime.giveMinutes(); // 10:30am will be 630 minutes

// save to sharedprefs
if(callChangeListener(time)){
persistInt(time);
if(getEditor().commit()) Log.d(lTag,"Successfully saved changes");
else Log.w(lTag,"Did NOT save changes");

}
}
//... more ...
}

在 perferences.xml 文件中,我可以使用 <com.example.TimePreference android:key="test"/>

关于android - 带有中性按钮的 TimePickerDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502248/

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