您的位置:首页 > 其它

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: