【问题标题】:Can't choose number from NumberPicker无法从 NumberPicker 中选择数字
【发布时间】:2014-11-12 19:38:21
【问题描述】:

我对 NumberPicker 有疑问。我创建了一个对话框,我想在其中放置一个 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>

截图

【问题讨论】:

标签: android android-xml android-dialog


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

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();
    }
}

【讨论】:

  • 它有效。能否请您简要解释一下为什么我的代码不起作用?
  • @6franek,我当然认为您的代码无法正常工作,因为您在 setView() 一次在查找数字选择器处两次膨胀了布局,因此两者都对 xml 进行了两个不同的引用,并且您给出了第一个布局对在对话框中显示的 setView 的膨胀参考和第二个布局膨胀参考以实现 setOnValueChangedListener 的侦听器编号选择器,间接引用另一个非对话框视图,因此您无法在对话框视图中获取编号选择器 onValueChange。
猜你喜欢
  • 2018-11-01
  • 2015-03-28
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多