偏好不起作用?

时间:2015-02-14 03:12:23

标签: java android xml

我在Activity中使用多个EditTexts,而不是使用sqlite,我使用SharedPreferences保存未加密的数据。但是,作为初学者,我不知道哪里出错了。当在stackoverflow中提到几乎相似的问题时,答案看起来都不同并且增加了更多的混乱。无论如何这里是我的Java代码:

package labs.dc.cart.cart;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;




public class MainActivity extends ActionBarActivity {

public static final String LOGTAG = "Cart";
public static final String SAVE = "saveText";
private SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    settings = getPreferences(MODE_PRIVATE);


    final Switch mySwitch = (Switch)findViewById(R.id.switchButton);
    mySwitch.setChecked(false);

    mySwitch.setOnCheckedChangeListener
    (newCompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged
   (CompoundButton buttonView, boolean isChecked)
        {

            if(isChecked)
            {
                LinearLayout pop =  
                (LinearLayout)findViewById(R.id.linear);
                pop.setVisibility(View.VISIBLE);
            }

            else
            {

                LinearLayout pop = 
                (LinearLayout)findViewById(R.id.linear);
                pop.setVisibility(View.INVISIBLE);
            }

        }
    });



}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

    public void save(View v)
{
    Log.i(LOGTAG,"clicked set");
    SharedPreferences.Editor editor = settings.edit();
    EditText Et = (EditText)findViewById(R.id.editTextOne);
    String Prefvalue = Et.getText().toString();
    editor.putString(LOGTAG, Prefvalue);
    editor.commit();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
} 

相应的EditText和按钮XML代码为:

 <ImageButton
    android:id="@+id/saveButton"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="save"/>

 <EditText
       android:id="@+id/editTextOne"
       android:inputType="text"
       android:layout_marginTop="110dp"
       android:background="#1ec0e9"
       android:alpha="0.5"
       android:ems="13"
       android:gravity="start"
       android:typeface="monospace"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

现在,我的问题是为什么偏好在这种情况下不起作用,哪种方法可以解决?

4 个答案:

答案 0 :(得分:1)

使用此选项存储首选项值

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

 // Writing data to SharedPreferences
 Editor editor = settings.edit();
 editor.putString("key", "some value");
 editor.commit();

答案 1 :(得分:0)

使用此代码

SharedPreferences mSharedPreferences =getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
        /*
         * TO WRITE VALUES INTO THE  PREFERENCE FILE
         */


        //get Editor object using  sharedPreference edit() Method

        Editor mEditor= mSharedPreferences.edit();
        //set boolean Values  into preference files

        mEditor.putBoolean("booleanValue", true);
        //set String value into the preference file
        mEditor.putString("stringValue", "hello");
        //set integer value into the preference file
        mEditor.putInt("intValue", 1);
        //set the long value into the preference file
        mEditor.putLong("longValue", 1212121);
        //set the float value into the preference file
        mEditor.putFloat("floatValue", 10.0f);
        //commit the changes
        mEditor.commit();


        /*
         * TO READ THE VALUES FROM THE PREFERENCE FILE
         */
        //get the boolean value from preference
        Boolean booleanValue=mSharedPreferences.getBoolean("boolean", true);
        //get the String value from  preference
        String stringValue=mSharedPreferences.getString("stringValue", "default value");
        //get the int value from preference
        int intValue=mSharedPreferences.getInt("intValue", 1);
        //get the long value from preference
        Long longValue=mSharedPreferences.getLong("longValue", 1212121);
        //get the float value from the preference
        Float floatValue=mSharedPreferences.getFloat("floatValue", 10.0f);

Detail

答案 2 :(得分:0)

据我所知,您没有从SharedPreferences中检索键值对。要检索相同内容,请使用onResume方法中的以下代码:

    SharedPreferences settings = getPreferences();// no arguments/ parameters // since preferences are single in  number
Et.setText(settings.getString(LOGTAG));

希望它有所帮助!祝你好运!

答案 3 :(得分:0)

在偏好设置中设置值:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
editor.putString("nickname", "Alen");
editor.putInt("id", 12);
editor.commit();

从偏好中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("nickname", "No name");
int idName = prefs.getInt("id", 0);
}