0

我正在使用自定义对话框首选项在我的首选项菜单中生成搜索栏。在研究了 seekbar 的实现之后,为了从 seekbar 中获取我需要的浮点值,我编写了以下代码:

public void onProgressChanged(SeekBar seek, int newValue,
        boolean fromTouch) {
    // Round the value to the closest integer value.
    if (stepSize >= 1) {
        value = Math.round(newValue/stepSize)*stepSize;

    }
    else {
        value = newValue;
    }

    // Set the valueText text.
    float sValue = newValue*10;
    sValue = sValue/100;

    valueText.setText(Float.toString(sValue));

这会产生我想要的浮动。但是,我希望能够在我的主要活动中使用这个浮动。我尝试使用 SharedPreferences 存储它:

userPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Editor editor = userPrefs.edit()
editor.putFloat("mpm", sValue);
editor.commit();

这就是我学会在扩展 Activity 的类中使用 SharedPreferences 的方式。

但是,由于此搜索栏扩展了对话首选项,我无法使用

getBaseContext()

当我收到该类型的方法 getBaseContext 未定义的错误时。

我曾尝试将 getBaseContext() 更改为 getContext() 但这并不成功,尽管这可能是因为我不熟悉此实现。

如何从对话首选项中保存此浮点数并在不同的类中使用该值?

我用来检索 SharedPreferences 的代码:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logbook);
    initialise();
    userPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    list = userPrefs.getString("list", "10");
    userswallstring = userPrefs.getString("height", "10.0");


    try {
        usersWall = Float.valueOf(userswallstring.trim());
    } catch (Exception e) {
        // TODO: handle exception
    }
    mpm = userPrefs.getFloat("mpm", 2);

Mpm.class:包com.gbclimber.ep;

 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.SharedPreferences;

 import android.content.SharedPreferences.Editor;
 import android.content.res.TypedArray;
 import android.preference.DialogPreference;
 import android.preference.PreferenceManager;

 import android.util.AttributeSet;

 import android.view.LayoutInflater;
 import android.view.View;

 import android.widget.SeekBar;
 import android.widget.TextView;


 public class Mpm extends
    DialogPreference implements SeekBar.OnSeekBarChangeListener {
// Layout widgets.
private SeekBar seekBar = null;
private TextView valueText = null;

// Custom xml attributes.
private int maximumValue = 0;
private int minimumValue = 0;
private int stepSize = 0;
private String units = null;

private int value = 0;
SharedPreferences userPrefs;
/**
 * The SeekBarDialogPreference constructor.
 * @param context of this preference.
 * @param attrs custom xml attributes.
 */
public Mpm(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray typedArray = context.obtainStyledAttributes(attrs,
        R.styleable.Mpm);

    maximumValue = typedArray.getInteger(
        R.styleable.Mpm_maximumValue, 0);
    minimumValue = typedArray.getInteger(
        R.styleable.Mpm_minimumValue, 0);
    stepSize = typedArray.getInteger(
        R.styleable.Mpm_stepSize, 1);
    units = typedArray.getString(
        R.styleable.Mpm_units);

    typedArray.recycle();
}
/**
 * {@inheritDoc}
 */
protected View onCreateDialogView() {

    LayoutInflater layoutInflater = LayoutInflater.from(getContext());

    View view = layoutInflater.inflate(
        R.layout.mpmdp, null);

    seekBar = (SeekBar)view.findViewById(R.id.seekbar);
    valueText = (TextView)view.findViewById(R.id.valueText);

    // Get the persistent value and correct it for the minimum value.
    value = getPersistedInt(minimumValue) - minimumValue;

    // You're never know...
    if (value < 0) {
        value = 0;
    }

    seekBar.setOnSeekBarChangeListener(this);
    seekBar.setKeyProgressIncrement(stepSize);
    seekBar.setMax(maximumValue - minimumValue);
    seekBar.setProgress(value);

    return view;
}
/**
 * {@inheritDoc}
 */
public void onProgressChanged(SeekBar seek, int newValue,
        boolean fromTouch) {
    // Round the value to the closest integer value.
    if (stepSize >= 1) {
        value = Math.round(newValue/stepSize)*stepSize;

    }
    else {
        value = newValue;
    }

    // Set the valueText text.
    float sValue = newValue*10;
    sValue = sValue/100;
    userPrefs = PreferenceManager
            .getDefaultSharedPreferences(getContext());
    Editor editor = userPrefs.edit();
    editor.putFloat("mpm", sValue);
    editor.commit();
    valueText.setText(Float.toString(sValue));

    callChangeListener(value);
}
/**
 * {@inheritDoc}
 */
public void onStartTrackingTouch(SeekBar seek) {
}
/**
 * {@inheritDoc}
 */
public void onStopTrackingTouch(SeekBar seek) {
}
/**
 * {@inheritDoc}
 */
public void onClick(DialogInterface dialog, int which) {
    // if the positive button is clicked, we persist the value.
    if (which == DialogInterface.BUTTON_POSITIVE) {
        if (shouldPersist()) {
            persistInt(value + minimumValue);
        }
    }

    super.onClick(dialog, which);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
    // TODO Auto-generated method stub
    super.onDialogClosed(positiveResult);
    persistInt(value + minimumValue);
}

}

4

1 回答 1

0

只是做了一个快速搜索,但为了你的上下文尝试

this.getDialog().getContext()
于 2012-08-17T19:45:25.923 回答