Android - 使用共享首选项发送数据不起作用

时间:2015-02-23 18:35:17

标签: android

我是Android编程新手,我只是尝试使用共享首选项将一些数据从一个活动发送到另一个活动,但我的代码不起作用。

这是我输入数据的活动。

public class MainActivity extends ActionBarActivity {

    private  Button  BTN;
    private EditText EDTXT,EDTXT2,EDTXT3;
    private CheckBox CHKBX;
    private final String mName = "Middle Name";
    private final String fName = "First Name";
    private final String lName = "Last Name";
    private final String isReg = "Is Registered";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BTN=(Button)findViewById(R.id.button);
    CHKBX=(CheckBox)findViewById(R.id.checkBox);
    EDTXT=(EditText)findViewById(R.id.editText);
    EDTXT2=(EditText)findViewById(R.id.editText2);
    EDTXT3=(EditText)findViewById(R.id.editText3);

    BTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(CHKBX.isChecked()){

                setPreferenceData();

                Intent INT = new Intent(MainActivity.this,SharedPreferenceData.class);
                startActivity(INT);

            }

        }
    });
}

public void setPreferenceData(){

    SharedPreferences sp = this.getSharedPreferences("MyPref",MODE_WORLD_READABLE);
    SharedPreferences.Editor spEditor = sp.edit();

    spEditor.putBoolean(isReg, CHKBX.isChecked());
    spEditor.putString(fName, EDTXT.getText().toString());
    spEditor.putString(mName, EDTXT2.getText().toString());
    spEditor.putString(lName, EDTXT3.getText().toString());
    spEditor.commit();


}

这是我想传递数据的活动。

public class SharedPreferenceData extends ActionBarActivity {

private TextView TXTVW,TXTVW1,TXTVW2,TXTVW3;
private final String fName = "First Name";
private final String mName = "Middle Name";
private final String lName = "Last Name";
private final String isReg = "Is Registered";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shared_preference_data);

    TXTVW = (TextView) findViewById(R.id.textView);
    TXTVW1 = (TextView) findViewById(R.id.textView2);
    TXTVW2 = (TextView) findViewById(R.id.textView3);
    TXTVW3 = (TextView) findViewById(R.id.textView4);

    getSharedPreferencesData();

}

public void getSharedPreferencesData(){

    SharedPreferences spf = this.getSharedPreferences("My Pref",MODE_WORLD_READABLE);

    TXTVW.setText("First Name:" + spf.getString(fName,""));
    TXTVW1.setText("Middle Name:" + spf.getString(mName,""));
    TXTVW2.setText("Last Name:" + spf.getString(lName,""));
    TXTVW3.setText("Is Registered:" + spf.getBoolean(isReg,true));

}

2 个答案:

答案 0 :(得分:1)

您可以在Intent

中传递数据
Intent intent = new Intent(MainActivity.this, SharedPreferenceData.class);
intent.putExtra(isReg, CHKBX.isChecked());
intent.putExtra(fName, EDTXT.getText().toString());
intent.putExtra(mName, EDTXT2.getText().toString());
intent.putExtra(lName, EDTXT3.getText().toString());
startActivity(intent);

然后在onCreate内(在SharedPreferenceData中)阅读:

Intent intent = getIntent();
TXTVW.setText("First Name:" + intent.getStringExtra(fName,""));
TXTVW1.setText("Middle Name:" + intent.getStringExtra(mName,""));
TXTVW2.setText("Last Name:" + intent.getStringExtra(lName,""));
TXTVW3.setText("Is Registered:" + intent.getBooleanExtra(isReg,true));

答案 1 :(得分:0)

您是否尝试过使用默认的SharedPreferences?

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

顺便说一下,如果你有一个常量类来放置常量以识别你要发送的数据会更好:

public final class Constants {

    // SHARED PREFERENCES
    public static final String SHARED_PREF_F_NAME = "First Name";
    public static final String SHARED_PREF_M_NAME = "Middle Name";
    public static final String SHARED_PREF_L_NAME = "Last Name";
    public static final String SHARED_PREF_IS_REG = "Is Registered";
}

然后你得到这些常数:

public void getSharedPreferencesData(){

    SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

    TXTVW.setText("First Name:" + spf.getString(Constants.SHARED_PREF_F_NAME,""));
    TXTVW1.setText("Middle Name:" + spf.getString(Constants.SHARED_PREF_M_NAME,""));
    TXTVW2.setText("Last Name:" + spf.getString(Constants.SHARED_PREF_L_NAME,""));
    TXTVW3.setText("Is Registered:" + spf.getBoolean(Constants.SHARED_PREF_IS_REG,true));

}