我遇到NumberPicker问题。我创建了一个Dialog,我想在其中放置一个NumberPicker。 NumberPicker显示,但我无法更改其中的数字。我设置了MinValue和MaxValue,我无法确定问题所在。
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();
}
}
<?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>
答案 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();
}
}