Numberpick的功能和用法

Numberpick的功能和用法

數(shù)值選擇器,用于用戶輸入數(shù)值,可通過鍵盤輸入數(shù)值,也可通過拖動來選擇數(shù)值

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);

? ? ? ? //設(shè)置np1的當前值

? ? ? ? np1.setValue(minfraction);

? ? ? ? //添加監(jiān)聽器

? ? ? ? np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

? ? ? ? ? ? //當numberPICKET發(fā)生改變的時候,會激發(fā)該方法

? ? ? ? ? ? @Override

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

? ? ? ? ? ? ? ? minfraction = newVal;

? ? ? ? ? ? ? ? showSelectedPrice();

? ? ? ? ? ? }

? ? ? ? });

? ? ? ? np2 = findViewById(R.id.numberpicket01);

? ? ? ? np2.setMinValue(145);

? ? ? ? np2.setMaxValue(150);

? ? ? ? //設(shè)置當前值

? ? ? ? np2.setValue(maxfraction);

? ? ? ? //添加監(jiān)聽器

? ? ? ? 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();

? ? }

}

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content">

? ? <TableRow

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content">

? ? ? ? <TextView

? ? ? ? ? ? android:layout_width="120dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="單科最小分數(shù)" />

? ? ? ? <NumberPicker

? ? ? ? ? ? android:id="@+id/numberpicket"

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="150dp"

? ? ? ? ? ? android:focusable="true"

? ? ? ? ? ? android:focusableInTouchMode="true" />

? ? </TableRow>

? ? <TableRow

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content">

? ? ? ? <TextView

? ? ? ? ? ? android:layout_width="120dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="單科最大分數(shù)"></TextView>

? ? ? ? <NumberPicker

? ? ? ? ? ? android:id="@+id/numberpicket01"

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="150dp"

? ? ? ? ? ? android:focusable="true"

? ? ? ? ? ? android:focusableInTouchMode="true" />

? ? </TableRow>

</TableLayout>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容