numberPicker实现时间选择设置功能
16lz
2021-01-26
<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.numberpicker.NumberPickerTest"> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="50dp" android:layout_gravity="center_horizontal" > android:id="@+id/hourpicker" android:layout_width="40dp" android:layout_height="wrap_content" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="时" /> android:id="@+id/minuteicker" android:layout_width="40dp" android:layout_height="wrap_content" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="分" />
NumberPickerTest:
package com.example.administrator.numberpicker;import android.app.Activity;import android.os.Bundle;import android.widget.NumberPicker;import android.widget.Toast;/** * Created by Administrator on 2016/10/11. */public class NumberPickerTest extends Activity implements NumberPicker.OnValueChangeListener,NumberPicker.OnScrollListener,NumberPicker.Formatter { private NumberPicker hourPicker; private NumberPicker minutePicker; private NumberPicker valuepicker; @Override protected void onCreate(Bundle bundle){ super.onCreate(bundle); setContentView(R.layout.main_activity); init(); } private void init() { hourPicker=(NumberPicker) findViewById(R.id.hourpicker); minutePicker=(NumberPicker) findViewById(R.id.minuteicker); // hourPicker.setFormatter(this); hourPicker.setOnValueChangedListener(this); hourPicker.setOnScrollListener(this); hourPicker.setMaxValue(24); hourPicker.setMinValue(0); hourPicker.setValue(9); // minutePicker.setFormatter(this); minutePicker.setOnValueChangedListener(this); minutePicker.setOnScrollListener(this); minutePicker.setMaxValue(60); minutePicker.setMinValue(0); minutePicker.setValue(49); /*valuepicker = (NumberPicker) findViewById(R.id.valuepicker); String[] city = {"立水桥","霍营","回龙观","龙泽","西二旗","上地"}; valuepicker.setDisplayedValues(city); valuepicker.setMinValue(0); valuepicker.setMaxValue(city.length - 1); valuepicker.setValue(4);*/ } @Override public String format(int value) { String tmpStr = String.valueOf(value); if (value < 10) { tmpStr = "0" + tmpStr; } return tmpStr; } @Override public void onScrollStateChange(NumberPicker view, int scrollState) { switch (scrollState) { case android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_FLING: Toast.makeText(this, "后续滑动(飞呀飞,根本停下来)", Toast.LENGTH_LONG) .show(); break; case android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_IDLE: Toast.makeText(this, "不滑动", Toast.LENGTH_LONG).show(); break; case android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: Toast.makeText(this, "滑动中", Toast.LENGTH_LONG) .show(); break; } } @Override public void onValueChange(NumberPicker numberPicker, int oldVal, int newVal) { Toast.makeText( this, "原来的值 " + oldVal + "--新值: " + newVal, Toast.LENGTH_SHORT).show(); }}
附:滑动数字变小参考
http://www.jb51.net/article/80816.htm
更多相关文章
- Android滑动解锁控件
- android srcollview按钮顶部停留
- 实现IOS版的抽屉效果(点击,拖拽滑动)
- (Android(安卓)View)底部导航栏—BottomNavigationView简单使用
- android Gallery点击 跳到ViewFlipper全屏显示图片
- Android(安卓)Custom View --- Continuous Slider(连续滑动器)
- android手势操作&&实现滑动切换activity
- [UI]抽屉菜单DrawerLayout分析(一)
- [android]滑动冲突 ScrollView+ViewPager+RecyclerView