gpt4 book ai didi

android - NumberPicker onValueChange 未被调用

转载 作者:行者123 更新时间:2023-11-29 01:26:43 25 4
gpt4 key购买 nike

你好,我在实现使用 NumberPickerDialogFragment 时遇到了一些问题。我有一些代码,如果用户按下按钮,将提示用户通过 AlertDialog 选择一个数字。我可以很好地显示 Dialog,但是当我通过 Picker 选择一个数字时,我无法保存该值。根据我的调试,似乎从未调用过 onValueChange 方法。任何想法为什么?下面是我的 numberPicker DialogFragment

代码
public static class numberPickerDialog extends DialogFragment
implements NumberPicker.OnValueChangeListener {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final NumberPicker picker = new NumberPicker(getActivity());
picker.setMinValue(1);
picker.setMaxValue(60);
picker.setValue(25);
picker.setWrapSelectorWheel(false);

builder.setMessage("Set Study Time")
.setPositiveButton("Set", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//cancel
}
});

final FrameLayout parent = new FrameLayout(getActivity());
parent.addView(picker, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER));
builder.setView(parent);

Dialog dialog = builder.create();
return dialog;
}

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
String toastMessage = "test";
Toast toast = Toast.makeText(getActivity(), toastMessage, Toast.LENGTH_SHORT);
toast.show();
}
}

最佳答案

您必须将 OnValueChangeListener 附加到 NumberPicker。

您所要做的就是:numberPicker.setOnValueChangedListener(this);

关于android - NumberPicker onValueChange 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617234/

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