NumberPicker简单功能用法
2016-04-28 10:21
351 查看
布局文件,简单没什么讲的
<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(); } }
相关文章推荐
- Hierarchical Cluster 层次聚类
- 分词URL
- SQLServer OpenRowSet 导入数据
- 【krpano】krpano xml资源解密(破解)软件说明与下载
- FFMPEG 处理RTMP流媒体的命令大全
- eventfd - create a file descriptor for event notification
- unity 多线程处理
- redis日志出现Can't save in background fork Cannot allocate memory
- Django 模板
- 利用Quart2D作一些简单的图形
- SVN Unable to connect to a repository at URL
- 使用maven创建web项目
- 基于空间金字塔池化的卷积神经网络物体检测
- 三 Python解释器
- Sql Server自动备份数据库,定期删除备份
- bat
- Unity3D学习笔记(三)为地形添加花草树木
- 全球域名解析新增保有量10强:万网排名退至第五
- Swoole介绍-真正的PHP Web开发框架
- 集成模型——随机森林