美文网首页Android 成长笔记
Android NumberPicker 使用示例

Android NumberPicker 使用示例

作者: 赵者也 | 来源:发表于2017-03-25 13:54 被阅读152次

    布局文件:

    <?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="match_parent"
        android:background="@color/colorGray"
        android:orientation="vertical"
        >
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <TextView
                android:text="@string/selectStart"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:gravity="center_vertical"
                android:layout_marginStart="10dp"
                />
    
            <NumberPicker
                android:id="@+id/numStart"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            >
            <TextView
                android:text="@string/selectEnd"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:gravity="center_vertical"
                android:layout_marginStart="10dp"
                />
    
            <NumberPicker
                android:id="@+id/numEnd"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                />
        </TableRow>
    
    </TableLayout>
    

    主程序的代码:

    package com.toby.personal.testlistview;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.NumberPicker;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        final private static String TAG = "Toby_Test";
    
        int startValue = 0;
        int endValue = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final NumberPicker numberPickerStart = (NumberPicker) findViewById(R.id.numStart);
            numberPickerStart.setMinValue(0);
            numberPickerStart.setMaxValue(50);
            numberPickerStart.setValue(startValue);
            numberPickerStart.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    startValue = newVal;
                    showCurrentValue();
                }
            });
    
            final NumberPicker numberPickerEnd = (NumberPicker) findViewById(R.id.numEnd);
            numberPickerEnd.setMinValue(51);
            numberPickerEnd.setMaxValue(100);
            numberPickerEnd.setValue(endValue);
            numberPickerEnd.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    endValue = newVal;
                    showCurrentValue();
                }
            });
        }
    
        private void showCurrentValue() {
            Toast.makeText(this, "Current Value is from " + startValue + " to " + endValue,
                    Toast.LENGTH_SHORT).show();
        }
    
    }
    

    运行效果:


    显示效果

    参考文献:《疯狂Android讲义(第2版)》

    相关文章

      网友评论

        本文标题:Android NumberPicker 使用示例

        本文链接:https://www.haomeiwen.com/subject/dkrcottx.html