Android的:如何使用NumberPickerDialog如何使用、Android、NumberPickerDialog

2023-09-06 07:22:30 作者:The heart of the heart 心之所向

可能有人给我如何在Activity.onCreateDialog实例化一个NumberPickerDialog的例子吗?(https://github.com/novak/numberpicker/blob/master/lib/src/com/michaelnovakjr/numberpicker/NumberPickerDialog.java) ?

Could someone give me an example of how to instantiate a NumberPickerDialog within Activity.onCreateDialog ?(https://github.com/novak/numberpicker/blob/master/lib/src/com/michaelnovakjr/numberpicker/NumberPickerDialog.java) ?

有一个回购实例名为numberpicker,演示了使用小工具,但没有实际的对话框。

There are examples in a repo called numberpicker-demo for using the widget, but none for the actual dialog.

在其他的方法我已经试过想是这样:

Amongst other approaches I've tried tried something like:

return new NumberPickerDialog.Builder(this)
    .setTitle("Choose Number")
    .etc..

但是,这仅仅是一个标准AlertDialog,没有NumberPicker。

But this just shows a standard AlertDialog, without the NumberPicker.

谢谢!

推荐答案

得到它的工作最终。有一个在com.quietlycoding.android.picker.Picker一个例子,但是我发现,对话不设置调光正常,发黑了整个活动的背景,而它的视图。

Got it working eventually. There's an example in com.quietlycoding.android.picker.Picker, but I've found that the dialog doesn't set the dimming properly, blacking out the whole Activity in the background while it's in view.

我工作围绕这一简单地创建一个AlertDialog以通常的方式,然后只是坚持一个NumberPicker小部件到的setView():

I worked around this by simply creating an AlertDialog in the usual way, and then just sticking a NumberPicker widget into setView():

LayoutInflater inflater = (LayoutInflater)
    getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View npView = inflater.inflate(R.layout.number_picker_pref, null);
    return new AlertDialog.Builder(this)
        .setTitle("Text Size:")
        .setView(npView)
        .setPositiveButton(R.string.dialog_ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .setNegativeButton(R.string.dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
            .create();

请务必从numberpicker项目复制number_picker_ pref.xml水库/布局自己的项目。

Make sure to copy number_picker_pref.xml from the numberpicker project to res/layout in your own project.