我AlertDialog只有一些文字,一个NumberPicker,一个OK,一个取消.
package org.dyndns.schep.example;
import android.os.Bundler;
import android.view.View;
import android.widget.NumberPicker;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
public class FooFragment extends DialogFragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mParent = (MainActivity) activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mParent.setFoo(foo());
}
})
.setNegativeButton(android.R.string.cancel, null);
View view = getActivity().getLayoutInflater.inflate(
R.layout.dialog_foo, null);
mPicker = (NumberPicker) view.findViewById(R.id.numberPicker1);
mPicker.setValue(mParent.getFoo()); …Run Code Online (Sandbox Code Playgroud) 在activity_main.xml:
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
Run Code Online (Sandbox Code Playgroud)
在MainActivity.java里面oncreate:
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker1.setMinValue(1);
numberPicker1.setMaxValue(20);
Run Code Online (Sandbox Code Playgroud)
当我用键盘编辑值时,我以编程方式获得的值不会改变,但如果我按下+然后-它可以工作.
如何在不按+和的情况下使其工作-?
编辑:
我用MelihAltıntaş的代码创建了一个新项目,它显示了一个全新的数字选择器!(用手指滑动但不能输入文字的那个).然后我和我的真实项目相比,我看到了android:theme="@android:style/Theme.NoTitleBar".我在新项目中添加了它,然后numberpicker成为我习惯的那个.