【问题标题】:Saving to SharedPreferences from custom DialogPreference从自定义 DialogPreference 保存到 SharedPreferences
【发布时间】:2011-01-16 05:36:20
【问题描述】:

我目前有一个首选项屏幕,并且我创建了一个扩展 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 说“方法 getDefaultSharedPreferences(AccDialog) 未定义 AccDialog 类型”。我尝试将上下文更改为我的首选项类,使用空白上下文,我还尝试命名我的 SharedPrefs 并使用getSharedPreferences()。我只是不确定我在这里做什么。

由于我对 Java/Android/编码很陌生,请您尽可能详细地提供帮助,例如。我需要在我的哪些文件中写入代码以及我应该在该文件中写入的位置(即onCreate()onClick() 等)

【问题讨论】:

    标签: java android sharedpreferences


    【解决方案1】:

    在您执行此行之前返回的onCreate() 中,因此Eclipse 说它无法访问。 在您的onClick() 中尝试:

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); 
    

    应该没问题

    【讨论】:

    • 我认为在“返回布局”之后不会有什么不同,因为它无论如何都在布局之外,但我将它移到“受保护的 View onCreateDialogView() {”之后的行问题消失了。它还解决了我在 onClick() 块中的问题。谢谢
    • 嗯,好的。在实施此修复后,我遇到了另一个问题,我编辑了上面的代码并在底部添加了一段来解释它。它似乎仍然没有按预期保存,或者它是但我没有正确阅读它。
    • 我觉得花了一整天时间来解决这个问题真是太愚蠢了,但我把 editor.commit() 排除在外,这就是为什么没有保存首选项的原因。您可以查看 300 次,但仍然会漏掉最简单的错误。
    【解决方案2】:
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
    

    【讨论】:

    • 我也试过了。在 onCreate() 代码中,Eclipse 说它是“无法访问的代码”,并建议我删除整行。在我的 onClick() 代码中,仍然存在与以前相同的错误。
    猜你喜欢
    • 2013-09-22
    • 2018-07-31
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多