Android Can';不要从NumberPicker中选择数字
我对NumberPicker有问题。我已经创建了一个对话框,我想在其中放置一个NumberPicker。显示NumberPicker,但我无法更改其中的数字。我已经设置了MinValue和MaxValue,但我不知道问题出在哪里 SettingsDialogFragment.java 设置\u dialog.xmlAndroid Can';不要从NumberPicker中选择数字,android,android-xml,android-dialog,Android,Android Xml,Android Dialog,我对NumberPicker有问题。我已经创建了一个对话框,我想在其中放置一个NumberPicker。显示NumberPicker,但我无法更改其中的数字。我已经设置了MinValue和MaxValue,但我不知道问题出在哪里 SettingsDialogFragment.java 设置\u dialog.xml 截图 试试这种方法,希望这能帮助你解决问题。 public class SettingsDialogFragment extends DialogFragment {
截图
试试这种方法,希望这能帮助你解决问题。
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()上已经膨胀了两次布局另一个在find number picker中,所以两个都对xml进行了两次不同的引用,您给了第一次布局膨胀引用setView,它显示在dialog中,第二次布局膨胀引用实现了setOnValueChangedListener,它间接引用了另一个not dialog视图,所以您无法获取数字对话框视图中的选择器onValueChange。
<?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>
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();
}
}