質問

I have two number pickers in a layout and I am using the layout for an AlertDialog Box. However the two number pickers do not have scrollers that are used to increase/decrease the value. Also I am not able to manually input values. Can anyone please help me find the bugs.

Dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout 
    android:id="@+id/llinner"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" >

    <NumberPicker
        android:id="@+id/attnp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout 
        android:id="@+id/llinnermost"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingTop="50sp">

        <TextView 
            android:id="@+id/innertv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="out of"
            android:textSize="20sp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp" />

    </LinearLayout>

    <NumberPicker
        android:id="@+id/attnp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>

Code snipper from activity that accesses this layout:

LayoutInflater lf = LayoutInflater.from(List_of_Courses.this);

            final View DialogView = lf.inflate(R.layout.dialog, null);


            final NumberPicker np1 = (NumberPicker) DialogView.findViewById(R.id.attnp1);
            final NumberPicker np2 = (NumberPicker) DialogView.findViewById(R.id.attnp2);
            np1.setValue(prevatt);
            np2.setValue(prevtot);



            final AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);
            alert.setTitle(cn+" - Attendance").setView(DialogView).setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int whichbutton) {

                            if(np1.getValue() == 0 || np2.getValue() == 0)return;
                            int att = np1.getValue();
                            int tot = np2.getValue();
                            if(att<=tot && tot>0){
                                db.UpdateAttendanceinSingleCourse(courseid, att, tot);
                                Toast.makeText(getApplicationContext(), "Attendance Updated", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                RaiseException();
                            }
                            db.close();
                            onResume();
                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int whichbutton) {
                            Log.v("CancelDialog", "User clicked Cancel");
                        }
                    });
            alert.show();
役に立ちましたか?

解決

If you do not provide min and maxvalues for NumberPicker, the increment/decrement buttons will not show up.

You may use setMaxValue and setMinValue like this:

np1.setMaxValue(MAX);
np1.setMinValue(MIN);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top