安卓学习第29课——numberPicker

摘要:
˂TableRowand
<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="match_parent" >
<TableRow 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="选择低价" />

    <NumberPicker
        android:id="@+id/np1"
        android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />
    
</TableRow>
<TableRow >

<TextView
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:text="选择高价" />

<NumberPicker
    android:id="@+id/np2"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />
</TableRow>

</TableLayout>

这里面用到了TableLayout

package com.example.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 {

    NumberPicker np1,np2;
    int minPrice=25,maxPrice=75;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        np1=(NumberPicker) findViewById(R.id.np1);
        np1.setMinValue(10);
        np1.setMaxValue(50);
        np1.setValue(minPrice);//设置当前值
        np1.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                minPrice=newVal;
                showSelectedPrice();
            }
            
        });
        np2=(NumberPicker) findViewById(R.id.np2);
        np2.setMinValue(60);
        np2.setMaxValue(100);
        np2.setValue(maxPrice);
        np2.setOnValueChangedListener(new OnValueChangeListener(){

            @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();
        
    }

}

记住需要设置最大值、最小值和当前值,还要有对应的事件监听。

免责声明:文章转载自《安卓学习第29课——numberPicker》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux升级gcc到最新版本--gcc-9.2.0Dart list add()和addAll方法使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

android studio里面怎么看file explorer啊?

1、打开DDMS ,不同的android studio 外观上有所不同,DDMS哪里打开呢? 如图,在“Tools”菜单下点击“Android”,找到“Android Device Monitor”。 2 点击“Android Device Monitor”,就可以打开DDMS啦。 2、先点击DDMS,在工具栏Window——>show...

appium简明教程(11)——使用resource id定位(仅支持安卓4.3以上系统)

上一节乙醇带大家了解了appium的定位策略。实际上appium的控件定位方式是完全遵守webdriver的mobile扩展协议的。 这一节将分享一下如何使用resource id来定位android策略。 什么是resource id,这个不属于本文的范畴,大家可以点这里了解。 我们可以有两种方式来使用resource id进行定位: 使用findEl...

android实现程序开机自启动

在安卓中,想要实现app开机自动启动,需要实现拦截广播android.permission.RECEIVE_BOOT_COMPLETED,并且需要使用静态注册广播的方法(即在AndroidManifest.xml文件中定义广播) 1、先在AndroidManifest.xml文件中定义广播和声明权限 <uses-permission android:...

人脸识别1:1对比 (二)

本项目采用Face++第三方接口,项目实现了两张图片的人脸识别和对比,得到相似度等信息 项目步骤如下: 一、所需权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permissio...

桌面小部件开发

AppWidgetProvider是android提供实现桌面小部件的类,本质是一个广播,即BroadcastReceiver。是继承关系 开发步骤: 1.在res/layout/下新建一个XML文件,命令为widget.xml,名称和内容可以自定义 <?xml version="1.0" encoding="utf-8"?> <Lin...

安卓智能聊天机器人开发(二)

接上一篇文章《安卓智能聊天机器人开发(一)》,晚上继续写。 在上一篇文章中,已经实现了对网络数据的获取和处理封装,这篇文章来讲下如何嵌入到安卓应用中。 先看下效果图: 从上面两张图我们可以发现,这个聊天布局其实就是一个ListView,只不过它和传统的ListView有些区别,因为它使用了多Item样式布局 首先,先来分析下基础布局: 这个界面是由3个布...