保存到共享preferences从定制对话框preference [英] Saving to SharedPreferences from custom DialogPreference

查看:112
本文介绍了保存到共享preferences从定制对话框preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前得到了preferences屏幕,我已经创建了一个扩展对话preference ,并从我的$中调用的自定义类p $ pferences。我的preferences数据似乎存储/从共享preferences 检索没有问题,但我想从<$ C加2多套的设置$ C>对话preference 。

I've currently got a preferences screen, and I've created a custom class that extends DialogPreference and is called from within my Preferences. My preferences data seems store/retrieve from SharedPreferences without an issue, but I'm trying to add 2 more sets of settings from the DialogPreference.

基本上我有,我一直没能找到的两个问题。我见过的每一个网站给了我同样的标准信息,以数据备份/恢复,我仍然有问题。首先我想保存到我的共享preferences (在code最后块可见)用户名和密码,如果有可能,我想要能做到这一点在的onClick()

Basically I have two issues that I have not been able to find. Every site I've seen gives me the same standard info to save/restore data and I'm still having problems. Firstly I'm trying to save a username and password to my SharedPreferences (visible in the last block of code) and if possibly I'd like to be able to do it in theonClick().

我的preferences XML调用我的对话preference

My preferences XML that calls my 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> 

我的自定义对话框preference类文件:

My Custom DialogPreference Class file:

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();
  }
 } 
}

我的主要活动测试code:

My main activity test code:

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();

    }

在我的共享preferences设置= preferenceManager.getDefaultShared preferences(本); 行,Eclipse中说的方法getDefaultShared preferences(AccDialog)是未定义的类型AccDialog。我已经尝试了上下文更改我的preferences类,使用空白背景和我也试命名我的共享preFS和使用 getShared preferences()为好。我只是不知道我在做什么在这里。

In my SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); line, Eclipse says "The method getDefaultSharedPreferences(AccDialog) is undefined for the type AccDialog". I've attempted to change the context to my preferences class, use a blank context and I've also tried naming my SharedPrefs and using getSharedPreferences() as well. I'm just not sure exactly what I'm doing here.

正如我一般是很新的Java / Android的/编码,你能不能请尽可能详细与任何帮助,例如。其中我的文件,我需要写code并在该文件的下落,我应该把它写(即的onCreate()的onClick( )等)

As I'm quite new to Java/Android/coding in general, could you please be as detailed as possible with any help, eg. which of my files I need to write the code in and whereabouts in that file should I write it (i.e. onCreate(), onClick(), etc)

推荐答案

的onCreate()你回来之前,执行这条线,所以Eclipse中说,这是不可达。
在你的的onClick()尝试:

In the onCreate() you returned before execute this line, so Eclipse says it's unreachable. In your onClick() try:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); 

应该是确定

这篇关于保存到共享preferences从定制对话框preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆