如何在Android中使用Timepicker仅显示小时

7

我有一个应用程序,需要选择仅以小时为单位的时间间隔范围,而不是像原生TimePicker提供的完整时间。我们如何自定义TimePicker,在Android中只允许我们选择和显示小时格式?


https://dev59.com/a2Ij5IYBdhLWcg3wfVF8 - Blaze Tama
3个回答

2

1
你可以使用Android Wheel。查看WheelView实现小时和分钟选择。
因此,您只需要保留一个WheelView,用于选择小时。例如:
final WheelView hours = (WheelView) findViewById(R.id.hourWheel);
hours.setViewAdapter(new NumericWheelAdapter(this, 0, 23));
hours.setLabel("hours");
hours.setLabelWidth(72);

忽略 Minute 选择的 WheelView。

还有各种适配器可用,包括 NumericWheelAdapter,它可以满足您的需求。 (为了防止链接在未来失效,下面将整个代码复制如下。)

/*
 *  Copyright 2011 Yuri Kanivets
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package kankan.wheel.widget.adapters;

import android.content.Context;

/**
 * Numeric Wheel adapter.
 */
public class NumericWheelAdapter extends AbstractWheelAdapter {

    /** The default min value */
    public static final int DEFAULT_MAX_VALUE = 9;

    /** The default max value */
    private static final int DEFAULT_MIN_VALUE = 0;

    // Values
    private int minValue;
    private int maxValue;

    // format
    private String format;

    /**
     * Constructor
     * @param context the current context
     */
    public NumericWheelAdapter(Context context) {
        this(context, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE);
    }

    /**
     * Constructor
     * @param context the current context
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     */
    public NumericWheelAdapter(Context context, int minValue, int maxValue) {
        this(context, minValue, maxValue, null);
    }

    /**
     * Constructor
     * @param context the current context
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     * @param format the format string
     */
    public NumericWheelAdapter(Context context, int minValue, int maxValue, String format) {
        super(context);

        this.minValue = minValue;
        this.maxValue = maxValue;
        this.format = format;
    }

    @Override
    public CharSequence getItemText(int index) {
        if (index >= 0 && index < getItemsCount()) {
            int value = minValue + index;
            return format != null ? String.format(format, value) : Integer.toString(value);
        }
        return null;
    }

    @Override
    public int getItemsCount() {
        return maxValue - minValue + 1;
    }    
}

希望它有所帮助。

0

我也在寻找解决这个问题的方法。最终想出了一个优雅的解决方案,能够按照我的要求工作。

class HourPickerDialog(
        context: Context,
        timeSetListener: OnTimeSetListener,
        initialHourOfDay: Int,
        initialMinute: Int = 0,
        is24HourView: Boolean = true,
) : TimePickerDialog(context, timeSetListener, initialHourOfDay, initialMinute, is24HourView) {

    override fun onTimeChanged(view: TimePicker, hourOfDay: Int, minute: Int) {
        onClick(this, BUTTON_POSITIVE)
        dismiss()
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接