numberPicker实现时间选择设置功能

来源:互联网 发布:网络与信息安全理解 编辑:程序博客网 时间:2024/05/21 17:20

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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">    <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginTop="30dp"    android:layout_marginLeft="50dp"    android:layout_gravity="center_horizontal" >        <NumberPicker        android:id="@+id/hourpicker"        android:layout_width="40dp"        android:layout_height="wrap_content" />        <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:text="时" />        <NumberPicker        android:id="@+id/minuteicker"        android:layout_width="40dp"        android:layout_height="wrap_content" />        <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:text="分" />    </LinearLayout></LinearLayout>
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

0 0
原创粉丝点击