我目前有一个偏好设置屏幕,我创建了一个扩展DialogPreference
的自定义类,并在我的偏好设置中调用。我的偏好设置数据似乎从SharedPreferences
存储/检索没有问题,但我正在尝试从DialogPreference
添加另外两组设置。
基本上我有两个我无法找到的问题。我见过的每个网站都给了我相同的标准信息来保存/恢复数据,我仍然遇到问题。首先,我正在尝试将用户名和密码保存到我的SharedPreferences
(在最后一段代码中可见),如果可能的话,我希望能够在onClick()
中执行此操作。
我的偏好XML调用我的DialogPreference
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<com.rone.optusmon.AccDialog
android:key="AccSettings"
android:title="Account Settings"
android:negativeButtonText="Cancel"
android:positiveButtonText="Save" />
</PreferenceCategory>
</PreferenceScreen>
我的自定义DialogPreference类文件:
package com.rone.optusmon;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AccDialog extends DialogPreference implements DialogInterface.OnClickListener {
private TextView mUsername, mPassword;
private EditText mUserbox, mPassbox;
CharSequence mPassboxdata, mUserboxdata;
private CheckBox mShowchar;
private Context mContext;
private int mWhichButtonClicked;
public AccDialog(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
protected View onCreateDialogView() {
// Access default SharedPreferences
@SuppressWarnings("unused")
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
@SuppressWarnings("unused")
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(10, 10, 10, 10);
layout.setBackgroundColor(0xFF000000);
mUsername = new TextView(mContext);
mUsername.setText("Username:");
mUsername.setTextColor(0xFFFFFFFF);
mUsername.setPadding(0, 8, 0, 3);
mUserbox = new EditText(mContext);
mUserbox.setSingleLine(true);
mUserbox.setSelectAllOnFocus(true);
mPassword = new TextView(mContext);
mPassword.setText("Password:");
mPassword.setTextColor(0xFFFFFFFF);
mPassbox = new EditText(mContext);
mPassbox.setSingleLine(true);
mPassbox.setSelectAllOnFocus(true);
mShowchar = new CheckBox(mContext);
mShowchar.setOnCheckedChangeListener(mShowchar_listener);
mShowchar.setText("Show Characters");
mShowchar.setTextColor(0xFFFFFFFF);
mShowchar.setChecked(false);
if(!mShowchar.isChecked()) {
mPassbox.setTransformationMethod(new PasswordTransformationMethod());
}
layout.addView(mUsername);
layout.addView(mUserbox);
layout.addView(mPassword);
layout.addView(mPassbox);
layout.addView(mShowchar);
return layout;
}
public void onClick(DialogInterface dialog, int which) {
mWhichButtonClicked = which;
// if statement to set save/cancel button roles
if (mWhichButtonClicked == -1) {
Toast.makeText(mContext, "Save was clicked\nUsername: " + mUserbox.getText().toString() +"\nPassword is: " + mPassbox.getText().toString(), Toast.LENGTH_SHORT).show();
// Save user preferences
SharedPreferences settings = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usernamekey", mUserbox.getText().toString());
editor.putString("passwordkey", mPassbox.getText().toString());
editor.commit();
}
else {
Toast.makeText(mContext, "Cancel was clicked", Toast.LENGTH_SHORT).show();
}
}
}
我的主要活动测试代码:
public void onResume() {
super.onResume();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("\nThe monitor will refresh every "+ pref.getString("refreshfreq", "30 minutes"));
builder.append("\nThe skin chosen is "+ pref.getString("skinkey", "null"));
builder.append("\nThe style chosen is "+ pref.getString("stylekey", "% used"));
builder.append("\nThe font chosen is "+ pref.getString("fontkey", "Calibri"));
builder.append("\nThe font color is "+ pref.getString("fontcolkey", "White"));
builder.append("\nYour username is "+ pref.getString("usernamekey", "not set yet"));
builder.append("\nYour password is "+ pref.getString("passwordkey", "not set yet"));
Toast.makeText(Optusmon.this, builder.toString(), Toast.LENGTH_LONG).show();
}
在我的SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
行中,Eclipse说“对于AccDialog类型,方法getDefaultSharedPreferences(AccDialog)是未定义的”。我试图将上下文更改为我的首选项类,使用空白上下文,我也尝试命名我的SharedPrefs并使用getSharedPreferences()
。我只是不确定我在这做什么。
由于我对Java / Android /编码一般都很陌生,如果有任何帮助,请尽可能详细说明,例如。我需要写哪些文件来编写代码和该文件中的行踪(即onCreate()
,onClick()
等)
答案 0 :(得分:3)
在执行此行之前返回的onCreate()
中,Eclipse说它无法访问。
在onClick()
尝试:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
应该没问题
答案 1 :(得分:0)
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);