Android的自定义数字Picker控件-NumberPicker使用方法
2015-04-26 21:24
471 查看
android-numberpicker是github的一个项目,提供了安卓中的自定义数字拾取控件,它的效果如图所示:
应用该控件也非常简单,使用如下几步即可:
1.从github上下载该控件,地址为:https://github.com/SimonVT/android-numberpicker;
2.将其中的library项目导入ADT;
3.在自己的project中引用该项目;
4.在要使用该控件的layout中找一个位置加入控件,格式如下:
5.通过如下代码操作此控件:
其中:NumberPicker类的setMaxValue、setMinValue、setDisplayedValues、getValue和setValue方法需要自己体会使用。
应用该控件也非常简单,使用如下几步即可:
1.从github上下载该控件,地址为:https://github.com/SimonVT/android-numberpicker;
2.将其中的library项目导入ADT;
3.在自己的project中引用该项目;
4.在要使用该控件的layout中找一个位置加入控件,格式如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <net.simonvt.numberpicker.NumberPicker android:id="@+id/numberPicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_getval" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="get value" /> </LinearLayout>
5.通过如下代码操作此控件:
package net.simonvt.numberpicker.samples; import net.simonvt.numberpicker.NumberPicker; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; /** * @author Simon Vig Therkildsen <simonvt@gmail.com> */ public class LightThemeActivity extends Activity { private LightThemeActivity c = this; private NumberPicker np; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_light); np = (NumberPicker) findViewById(R.id.numberPicker); String[] values = new String[]{"10","20","30","40","50","60","70","80","90","100","110","120","130","140","150","160","170","180","190","200","210","220","230","240","250"}; np.setMaxValue(values.length - 1); np.setMinValue(0); np.setDisplayedValues(values); np.setFocusable(true); np.setFocusableInTouchMode(true); this.setNumberPickerValue(30); Button btn_getvalue = (Button) this.findViewById(R.id.btn_getval); btn_getvalue.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(c, "value = "+getNumberPickerValue(), Toast.LENGTH_SHORT).show(); } }); } public int getNumberPickerValue(){ if(np != null){ return (np.getValue()+1)*10; }else{ return -1; } } public void setNumberPickerValue(int val){ if(np != null){ np.setValue(val/10-1); } } }
其中:NumberPicker类的setMaxValue、setMinValue、setDisplayedValues、getValue和setValue方法需要自己体会使用。
相关文章推荐
- Android常用控件-DatePicker以及对话框的两种使用方法
- Android常用控件-DatePicker以及对话框的两种使用方法
- Android自定义组合控件以及使用方法_1
- android自定义粘性控件,综合使用measure,layout,onTouchEvent,onInterceptTouchEvent等方法
- Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法
- DatePicker 控件的使用 android 开发
- Android开发进阶自定义控件之滑动开关实现方法【附demo源码下载】
- Android编程——自定义View类onSaveInstanceState与onRestoreInstanceState使用方法
- android --activity 中的控件设置侦听(onclickListener)无效,自定义adapter不执行getView 方法
- Android使用GridLayout绘制自定义日历控件
- QT自定义函数使用UI控件的简单方法
- Android开发中使用WebView控件显示找不到网页的解决方法
- Android APP使用自定义字体实现方法
- delphi xe6 for android 自带控件LocationSensor优先使用GPS定位的方法
- 使用Android原生控件自定义Switch控件
- android 中使用回调方法(适用于自定义view传值到activity、adapter传值到activity)
- Android自定义属性时TypedArray的使用方法
- android DatePickerDialog TimePickerDialog set方法执行的不规律解决以及 DatePicker TimePicker的使用
- Android ListView控件的使用方法
- Android搜索控件的基本使用方法