Android应用设置为24小时

时间:2015-01-27 15:13:55

标签: java android draw android-preferences

我试图在我的应用中添加该选项24小时(国际)时间。目前,在12小时(AM / PM)模式下绘制BottomLabel和TopLabel的时间。我已将此选项添加到设置菜单中,但现在需要在选择“首选项”时实施该选项。怎么样???

我想我需要一个if语句来检查Prefence,但我不确定;请参阅代码中的WHAT GOES HERE???。以下是相关代码。提前感谢您的帮助!

在preferences.xml中:

<PreferenceCategory android:title="@string/pref_other_category">
    <ListPreference
        android:title="@string/pref_hour_category"
        android:defaultValue="h"
        android:entries="@array/arr_hr_type_entries"
        android:entryValues="@array/arr_data_refresh_rate_values"
        android:key="hour_format" />
</PreferenceCategory>

我转换时间格式并在TimeSliderView.java中绘制标签:

/**
 * Get the current aggregation type based on the date range
 */
@SuppressLint("SimpleDateFormat")
public void setAggregation(AggregationType aggregation) {
    _aggregation = aggregation;
    // Update date format symbols converting "AM/PM" to "a/p"
    DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
    symbols.setAmPmStrings(new String[]{"a", "p"});
    // Configure date formatter based on new aggregation type
    switch (_aggregation) {
        case Hour:
            _sdf = new SimpleDateFormat("h:mm", symbols);
            break;
        case HalfDay:
            _sdf = new SimpleDateFormat("h:mma", symbols);
            break;
        case Day:
            if (WHAT GOES HERE???) {
                _sdf = new SimpleDateFormat("Ha", symbols);
            } else{
                _sdf = new SimpleDateFormat("ha", symbols);
            }
            break;
        case Week:
            _sdf = new SimpleDateFormat("E", symbols);
            break;
    }
    invalidate();
}

/**
 * Draw the given time at the given position
 *
 * @param canvas the canvas on which the background will be drawn
 * @param time   The timestamp in milliseconds to draw
 */
private void drawLabel(Canvas canvas, long time, float left, float top, float right,
        float bottom) {

    if (_showBottomLabel || _showTopLabel) {
        _calendar.setTimeInMillis(time);
        String labelStr = _sdf.format(_calendar.getTime());
        _labelRect.set(left, top, right, bottom);
        // Draw  top label
        if (_showTopLabel) {
            canvas.drawText(labelStr, _labelRect.left, _labelRect.top + _labelCenterTop,
                    _textPaint);
        }
        // Draw  bottom label
        if (_showBottomLabel) {
            canvas.drawText(labelStr, _labelRect.left, _labelRect.bottom - _labelCenterBottom,
                    _textPaint);
        }
    }
}

/**
 * Draw labels top and bottom background on the given canvas
 */
private void drawLabelsBackground(Canvas canvas, long time, float left, float top, float right,
        float bottom) {

    if (_showBottomLabel || _showTopLabel) {
        // Check for next bar therefore add one interval to time
        boolean isClosed = !TaurusApplication.getMarketCalendar().isOpen(new Date(time + DataUtils.METRIC_DATA_INTERVAL));
        if (_showBottomLabel) {
            _labelRect.set(left, bottom - _labelHeight, right, top + getHeight());
            canvas.drawRect(_labelRect, isClosed ? _darkPaint : _lightPaint);
            // Draw label divider lines
            if (!_collapsed || !isClosed) {
                _labelRect.bottom = _labelRect.top + 1;
                canvas.drawRect(_labelRect, _strokePaint);
            }
        }
        if (_showTopLabel) {
            _labelRect.set(left, top, right, top + _labelHeight);
            canvas.drawRect(_labelRect, isClosed ? _darkPaint : _lightPaint);
            // Draw label divider lines
            if (!_collapsed || !isClosed) {
                _labelRect.top = _labelRect.bottom - 1;
                canvas.drawRect(_labelRect, _strokePaint);
            }
        }
    }
}

0 个答案:

没有答案