数值选择器NumberPicker

数值选择器是用于让用户输入数值,下面有三个方法:
setMinValue(int minVal):设置该组件的最小值
setManValue(int minVal):设置该组件最大值
setValue(int Value):设置该组件的值
xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="先择低价"
         android:textSize="30dp" />
     <NumberPicker
         android:id="@+id/np1"
         android:layout_width="match_parent"
         android:layout_height="80dp"
         android:focusable="true"
         android:focusableInTouchMode="true"/>

 </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="先择高价"
            android:textSize="30dp" />
        <NumberPicker
            android:id="@+id/np2"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:focusable="true"
            android:focusableInTouchMode="true"/>

    </LinearLayout>
</LinearLayout>

注释:如果页面上有EditText等控件,开发者又没做其他处理,那么用户打开该页面时往往会自动弹出输入法。这是以为编辑框会默认获得焦点,即默认模拟用户的点击操作,于是输入法的软键盘就弹出来了,想要避免这种情况,就得阻止编辑框默认获得焦点。比较常见的做法是给该页面的根节点设置focusable和focusableInTouchMode属性,通过将这两个属性设置为true可以强制让根节点获得焦点,从而避免输入法自动弹出的尴尬

Activity:

package com.example.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class MainActivity extends AppCompatActivity {
    private int minprice=25;
    private int maxprice=75;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NumberPicker np1=findViewById(R.id.np1);
        //设置np1的最大值和最小值
        np1.setMaxValue(50);
        np1.setMinValue(10);
        //设置np1的当前值
        np1.setValue(minprice);
        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            //当NunberPicker的值发生改变时,将会激发该方法
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
               minprice=newVal;
               showSelectedPrice();
            }
        });
    NumberPicker np2=findViewById(R.id.np2);
        np2.setMaxValue(100);
        np2.setMinValue(60);
        np2.setValue(maxprice);
        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            //当NunberPicker的值发生改变时,将会激发该方法
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                maxprice=newVal;
                showSelectedPrice();
            }
        });
    }
    private void showSelectedPrice()
    {
        Toast.makeText(this,"您选择的最低价格为"+minprice+",最高价格为:"+maxprice,Toast.LENGTH_SHORT).show();
    }

}

效果图如下:


2.gif

看到这里也许你发现了个问题就是Toast没有实时变化,而是没滚动一下就变化一下,这主要是我在每一个滚动都设置了显示,代码你可以自己改良。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 安卓原生自带控件setMinValue(int minVal):设置该组件支持的最小值。setMaxValue(i...
    babybus_hentai阅读 3,001评论 1 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,664评论 0 17
  • Web网站测试流程和方法(转载) 1测试流程与方法 1.1测试流程 进行正式测试之前,应先确定如何开展测试,不可盲...
    夏了夏夏夏天阅读 1,326评论 0 0
  • 爱情和婚姻? 既然完全不是一回事,为什么要有爱情呢? 让想要爱情的人拥有爱情,而想要婚姻的人拥有婚姻。 二者还是不...
    七个H阅读 33评论 0 0
  • 我说过我不需要你,没想到到头来却还是要依靠你,这是不是一种讽刺,是我无能的一种表现。我可以败一次,两次,很多次,一...
    虚无0阅读 90评论 0 1