我在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();
}
答案 0 :(得分: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);
}
});