无法从NumberPicker中选择号码

时间:2014-09-18 12:49:48

标签: android android-xml android-dialog

我遇到NumberPicker问题。我创建了一个Dialog,我想在其中放置一个NumberPicker。 NumberPicker显示,但我无法更改其中的数字。我设置了MinValue和MaxValue,我无法确定问题所在。

SettingsDialogFragment.java

public class SettingsDialogFragment extends DialogFragment{

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.settings);
        builder.setView(inflater.inflate(R.layout.settings_dialog, null))
            .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   }
               })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   }
               });
        final NumberPicker np = (NumberPicker) inflater.inflate(R.layout.settings_dialog, null).findViewById(R.id.weight_picker);
        np.setMaxValue(300);
        np.setMinValue(0);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(( new NumberPicker.
        OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("value is",""+newVal);
            }
        }));
        return builder.create();
    }
}

settings_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <NumberPicker 
        android:id="@+id/weight_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

</LinearLayout>

截图

screenshot with dialog and numberpicker

1 个答案:

答案 0 :(得分:3)

请尝试这种方式,希望这有助于您解决问题。

public class SettingsDialogFragment extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.settings);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.settings_dialog, null);

        builder.setView(view);
        builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
         NumberPicker np = (NumberPicker) view.findViewById(R.id.weight_picker);
        np.setMaxValue(300);
        np.setMinValue(0);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(( new NumberPicker.
                OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("value is",""+newVal);
            }
        }));
        return builder.create();
    }
}