savedPreferences未被发送

时间:2011-07-18 07:24:26

标签: java android

这是我的注册码

    package com.synamegames.lolingaddress;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class RegisterActivity extends Activity {
    TextView vname, vphone, vemail, vaddress, v_name,v_phone,v_email,v_address
    ,reg_name,reg_phone,reg_email,reg_address;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registermenu);
        vname=(TextView)findViewById(R.id.reg_name);
        vphone = (TextView) findViewById(R.id.reg_phone);
        vemail = (TextView) findViewById(R.id.reg_email);
        vaddress = (TextView) findViewById(R.id.reg_address);
        LoadPreferences();
    }

        private void LoadPreferences() {
            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
            String strSavedMem1 = sharedPreferences.getString("MEM1", "");
            String strSavedMem2 = sharedPreferences.getString("MEM2", "");
            String strSavedMem3 = sharedPreferences.getString("MEM3", "");
            String strSavedMem4 = sharedPreferences.getString("MEM4", "");
            String deviceid = sharedPreferences.getString("Devid", "");
            vname.setText(strSavedMem1);
            vphone.setText(strSavedMem2);
            vemail.setText(strSavedMem3);
            vaddress.setText(strSavedMem4); // textfield

    ;
}

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                Intent intent = new Intent(RegisterActivity.this, MainMenuActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
                return super.onKeyDown(keyCode, event);
            }
            return super.onKeyDown(keyCode, event);
        }



    }

在我的设置类中识别出Mem 1和其他字符串,我看到它们已加载。 sharedPreference的内容是在多个类中共享的吗?

现在,当我打开此活动时,它会显示我

名称: (如果有偏好) 电话: (如果有偏好) 电子邮件: (如果有偏好) 地址: (如果有偏好)

(首选项应该在哪里)是空白的,我希望将文本设置为我加载的内容。

感谢您的帮助!

保存首选项时的代码

                        SavePreferences("MEM1", vname.getText().toString());
                    SavePreferences("MEM2", vphone.getText().toString());
                    SavePreferences("MEM3", vemail.getText().toString());
                    SavePreferences("MEM4", vaddress.getText().toString());

        private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

1 个答案:

答案 0 :(得分:0)

getPreferences会返回此特定活动的私有首选项。如果您在其他活动中设置此偏好设置,例如使用PreferenceActivity,那么它们就不会以这种方式提供。在这种情况下,您可以使用getDefaultSharedPreferences