Android NumberPicker 使用示例

布局文件:

<?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 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,644评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,759评论 0 17
  • 天空布满了云, 山风狂野地吹。 老带幼来了, 坟地已杂草横生。 老者用力锄草, 幼儿在旁折枝摘花嘻戏。 铲草砍杂树...
    自由的充电阅读 271评论 0 0
  • 万佛在沙漠的边缘刻下死亡 今日我与之邂逅 厨房外墙角的泥土里 一只老鼠将自己今生的骨殖 驼入来世的花开 我曾见到一...
    钟政阅读 246评论 0 1