NumberPicker未在AlertDialog中显示

Par*_*xis 5 user-interface android components android-alertdialog numberpicker

我正在尝试将一个NumberPicker添加到AlertDialog中,但即使看起来没有错误也没有apper.我是Android编码的新手,所以我认为这是一个非常愚蠢的东西.

        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        alert.setTitle("Select the value: ");

        NumberPicker np = new NumberPicker(MainActivity.this);
        String[] nums = new String[100];
        for(int i=0; i<nums.length; i++)
               nums[i] = Integer.toString(i);

        np.setMinValue(1);
        np.setMaxValue(nums.length-1);
        np.setWrapSelectorWheel(false);
        np.setDisplayedValues(nums);
        np.setValue(50);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          // Do something with value!
          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Cancel.
          }
        });

        alert.show();
Run Code Online (Sandbox Code Playgroud)

Vas*_*dev 4

添加alert.setView(np); 您必须告诉警报对话框设置新视图。