2

我对 Android 中的数字选择器有疑问。它仅在两条水平线之间显示 0 值。看起来我试图在代码中设置的设置不起作用。我的 xml 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <NumberPicker
        android:id="@+id/np"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"/>
</LinearLayout>

我的 DialogFragment 代码:

public class MyDialogFragment extends DialogFragment{

    public static MyDialogFragment newInstance(int title) {
        MyDialogFragment frag = new MyDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        // Create the array of numbers that will populate the numberpicker
            final String[] nums = new String[21];
            for(int i=0; i<nums.length; i++) {
               nums[i] = Integer.toString(i*5);
            }



     // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.scaledialog, null);

      //set up number picker
        NumberPicker np = (NumberPicker) view.findViewById(R.id.np);
        np.setMaxValue(20);
        np.setMinValue(5);
        np.setWrapSelectorWheel(true);
        np.setDisplayedValues(nums);
        np.setValue(5);

        return new AlertDialog.Builder(getActivity())
                .setView(inflater.inflate(R.layout.scaledialog, null))
                //.setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                           // ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            //((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
    }
4

1 回答 1

0

尝试这个 !!

建立对话框后,设置应该排队

所以使用Post()Runnable()来实现这一点。

    final NumberPicker np = (NumberPicker) view.findViewById(R.id.np);

    np.post(new Runnable() {
        @Override
        public void run() {

            String[] nums = new String[21];
            for(int i=0; i<nums.length; i++) {
                nums[i] = Integer.toString(i*5);
            }

            np.setMaxValue(20);
            np.setMinValue(5);
            np.setWrapSelectorWheel(true);
            np.setDisplayedValues(nums);
            np.setValue(5);
        }
    });
于 2014-07-27T04:49:20.763 回答