Numberpick的功能和用法

Numberpick的功能和用法

数值选择器,用于用户输入数值,可通过键盘输入数值,也可通过拖动来选择数值

public class MainActivity extends Activity {

    NumberPicker np1, np2;

    //定义最小值,最大值的初始值

    int minfraction = 140, maxfraction = 150;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.numberpicket);

        np1 = findViewById(R.id.numberpicket);

        np1.setMaxValue(150);

        np1.setMinValue(140);

        //设置np1的当前值

        np1.setValue(minfraction);

        //添加监听器

        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            //当numberPICKET发生改变的时候,会激发该方法

            @Override

            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                minfraction = newVal;

                showSelectedPrice();

            }

        });

        np2 = findViewById(R.id.numberpicket01);

        np2.setMinValue(145);

        np2.setMaxValue(150);

        //设置当前值

        np2.setValue(maxfraction);

        //添加监听器

        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override

            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                minfraction = newVal;

                showSelectedPrice();

            }

        });

    }

    private void showSelectedPrice() {

        Toast.makeText(this, "你的单科最低分是" + minfraction + "你单科的最高分是" + maxfraction, Toast.LENGTH_LONG).show();

    }

}

    android:layout_width="match_parent"

    android:layout_height="wrap_content">

   

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

       

            android:layout_width="120dp"

            android:layout_height="wrap_content"

            android:text="单科最小分数" />

       

            android:id="@+id/numberpicket"

            android:layout_width="match_parent"

            android:layout_height="150dp"

            android:focusable="true"

            android:focusableInTouchMode="true" />

   

   

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

       

            android:layout_width="120dp"

            android:layout_height="wrap_content"

            android:text="单科最大分数">

       

            android:id="@+id/numberpicket01"

            android:layout_width="match_parent"

            android:layout_height="150dp"

            android:focusable="true"

            android:focusableInTouchMode="true" />

   

你可能感兴趣的:(Numberpick的功能和用法)