NumberPicker简单功能用法

这里写图片描述

布局文件,简单没什么讲的

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="选择低价" />
        <NumberPicker android:id="@+id/np1"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:focusable="true"
            android:focusableInTouchMode="true"/>    
    </TableRow>
    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="选择高价" />
        <NumberPicker android:id="@+id/np2"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:focusable="true"
            android:focusableInTouchMode="true"/> 
        </TableRow>
</TableLayout>

代码部分

package com.test.numberpicker;

import android.app.Activity;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private NumberPicker np1,np2;
    //定义默认显示的最低最高价
    int minprice = 25;
    int maxprice = 75;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        np1 = (NumberPicker)findViewById(R.id.np1);
        np2 = (NumberPicker)findViewById(R.id.np2);
        //设置最低价区间
        np1.setMaxValue(50);
        np1.setMinValue(0);
        np1.setValue(minprice);
         //设置最高价区间
        np2.setMinValue(51);
        np2.setMaxValue(100);
        np2.setValue(maxprice);
        //监听NumberPicker,当值发生变化会调用下面方法
        np1.setOnValueChangedListener(new OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                minprice = newVal;
                showprice();
            }
        });
        np2.setOnValueChangedListener(new OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                maxprice = newVal;
                showprice();
            }
        });
    }

    protected void showprice() {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "最低价为"+minprice+"最高价为"+maxprice, Toast.LENGTH_LONG).show();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值