簡體   English   中英

從自定義DialogPreference保存到SharedPreferences

[英]Saving to SharedPreferences from custom DialogPreference

我目前有一個首選項屏幕,我已經創建了一個擴展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中,Eclipse說“對於AccDialog類型,方法getDefaultSharedPreferences(AccDialog)是未定義的”。 我試圖將上下文更改為我的首選項類,使用空白上下文,我也嘗試命名我的SharedPrefs並使用getSharedPreferences() 我只是不確定我在這做什么。

由於我對Java / Android /編碼一般都很陌生,請您盡可能詳細地提供任何幫助,例如。 我需要在哪個文件中編寫代碼和該文件中的位置(我應該寫它)(即onCreate()onClick()等)

在執行此行之前返回的onCreate() ,Eclipse表示它無法訪問。 在你的onClick()嘗試:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); 

應該可以

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM