java - xml 中的自定义值

标签 java android xml preference

<分区>

res/xml/prefs.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
     <com.estysoft.android.preference.SeekBarPreference
            android:dialogMessage="Something duration" 
            android:key="duration" 
            android:summary="How long something will last" 
            android:text=" minutes" 
            android:min="5" 
            android:defaultValue="5" 
            android:max="60" 
            android:title="Duration of something"/>
</PreferenceScreen>

我的问题是 android:min="5"给出了 error: No resource identifier found for attribute 'min' in package 'android'. 如何为我的自定义设置最小值SeekBarPreference?

编辑,我将 SeekBarPreference 源添加到

package com.estysoft.android.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout;


public class SeekBarPreference extends DialogPreference implements
        SeekBar.OnSeekBarChangeListener {
    private static final String androidns = "http://schemas.android.com/apk/res/android";

    private SeekBar mSeekBar;
    private TextView mSplashText, mValueText;
    private Context mContext;

    private String mDialogMessage, mSuffix;
    private int mDefault, mMax, mMin, mValue = 0;

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
        mSuffix = attrs.getAttributeValue(androidns, "text");
        
        mMax = attrs.getAttributeIntValue(androidns, "max", 100);
        mMin = attrs.getAttributeIntValue(androidns, "min", 0);
        
        mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", mMin);

    }

    @Override
    protected View onCreateDialogView() {
        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(6, 6, 6, 6);

        mSplashText = new TextView(mContext);
        if (mDialogMessage != null)
            mSplashText.setText(mDialogMessage);
        layout.addView(mSplashText);

        mValueText = new TextView(mContext);
        mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
        mValueText.setTextSize(32);
        params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(mValueText, params);

        mSeekBar = new SeekBar(mContext);
        
        mSeekBar.setOnSeekBarChangeListener(this);
        layout.addView(mSeekBar, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        if (shouldPersist())
            mValue = getPersistedInt(mDefault);

        mSeekBar.setMax(mMax-mMin);
        mSeekBar.setProgress(mValue-mMin);
        return layout;
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        mSeekBar.setMax(mMax-mMin);
        mSeekBar.setProgress(mValue-mMin);
    }

    @Override
    protected void onSetInitialValue(boolean restore, Object defaultValue) {
        super.onSetInitialValue(restore, defaultValue);
        if (restore)
            mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
        else
            mValue = (Integer) defaultValue;
    }

    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        String t = String.valueOf(value+mMin);
        mValueText.setText(mSuffix == null ? t : t.concat(mSuffix));
        if (shouldPersist())
            persistInt(value);
        callChangeListener(new Integer(value));
    }

    public void onStartTrackingTouch(SeekBar seek) {
    }

    public void onStopTrackingTouch(SeekBar seek) {
    }

    public void setMax(int max) {
        mMax = max;
    }

    public int getMax() {
        return mMax;
    }

    public void setMin(int min) {
        mMin = min;
    }

    public int getMin() {
        return mMin;
    }

    public void setProgress(int progress) {
        mValue = progress;
        if (mSeekBar != null)
            mSeekBar.setProgress(progress-mMin);
    }

    public int getProgress() {
        return mMin + mValue;
    }
}

最佳答案

您需要在使用前定义自定义 xml 属性。

在您的 res/values/attrs.xml 文件中:

<resources>
    <declare-styleable name="SeekBarPreferenceAttrs">
        <attr name="min" format="integer" />
    </declare-styleable>
</resources>

关于java - xml 中的自定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737404/

相关文章:

java - 依赖maven中的模块

java - DTO 的更好方法?

android - 更改图层列表项中的矢量可绘制颜色

android - xml样式动态换色

java - 从Spring创建war文件:boot project in Eclipse

Java - 在 Graphics2D 中获取新的旋转点

安卓 WebView 客户端

Mac 上的 Android Studio,如何修复空白处的 "Unexpected token"?

java - Android usb主机与定制板的串行通信

python - 使用 python 从多个 xml 文件中提取数据