1
我創建了一個代碼,當我有一個常規按鈕名爲「@ + id/buttonSave_character」 但是,當我刪除它並添加圖像按鈕並嘗試保存我存儲的值edittext,它不會這樣做。代碼有什麼問題?Android Sharedpreferences不會保存
另一個奇怪的是,我可以看到,我還沒有定義代碼中的按鈕,但它仍然會保存它(與舊的常規按鈕)。
package com.daniel.darkheresy.character;
import com.daniel.darkheresy.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Character_Activity extends Activity {
TextView name ;
TextView home_world;
TextView weapon_skill;
TextView ballistic_skill;
TextView strength;
TextView toughness;
TextView agility;
TextView intelligence;
TextView perception;
TextView will_power;
TextView fellowship;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Home_world = "home_worldKey";
public static final String Weapon_skill = "weapon_skillKey";
public static final String Ballistic_skill = "ballistic_skillKey";
public static final String Strength = "strengthKey";
public static final String Toughness = "toughnessKey";
public static final String Agility = "agilityKey";
public static final String Intelligence = "intelligenceKey";
public static final String Perception = "perceptionKey";
public static final String Will_power = "will_powerKey";
public static final String Fellowship = "fellowshipKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.character_layout);
name = (TextView) findViewById(R.id.editTextName);
home_world = (TextView) findViewById(R.id.editTextHome_world);
weapon_skill = (TextView) findViewById(R.id.editTextWeapon_skill);
ballistic_skill = (TextView) findViewById(R.id.editTextBallistic_skill);
strength = (TextView) findViewById(R.id.editTextStrength);
toughness = (TextView) findViewById(R.id.editTextToughness);
agility = (TextView) findViewById(R.id.editTextAgility);
intelligence = (TextView) findViewById(R.id.editTextIntelligence);
perception = (TextView) findViewById(R.id.editTextPerception);
will_power = (TextView) findViewById(R.id.editTextWill_power);
fellowship = (TextView) findViewById(R.id.editTextFellowship);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name))
{
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Home_world))
{
home_world.setText(sharedpreferences.getString(Home_world, ""));
}
if (sharedpreferences.contains(Weapon_skill))
{
weapon_skill.setText(sharedpreferences.getString(Weapon_skill, ""));
}
if (sharedpreferences.contains(Ballistic_skill))
{
ballistic_skill.setText(sharedpreferences.getString(Ballistic_skill, ""));
}
if (sharedpreferences.contains(Strength))
{
strength.setText(sharedpreferences.getString(Strength,""));
}
if (sharedpreferences.contains(Toughness))
{
toughness.setText(sharedpreferences.getString(Toughness,""));
}
if (sharedpreferences.contains(Agility))
{
agility.setText(sharedpreferences.getString(Agility,""));
}
if (sharedpreferences.contains(Intelligence))
{
intelligence.setText(sharedpreferences.getString(Intelligence,""));
}
if (sharedpreferences.contains(Perception))
{
perception.setText(sharedpreferences.getString(Perception,""));
}
if (sharedpreferences.contains(Will_power))
{
will_power.setText(sharedpreferences.getString(Will_power,""));
}
if (sharedpreferences.contains(Fellowship))
{
fellowship.setText(sharedpreferences.getString(Fellowship,""));
}
}
public void run(View view){
String n = name.getText().toString();
String hw = home_world.getText().toString();
String ws = weapon_skill.getText().toString();
String bs = ballistic_skill.getText().toString();
String s = strength.getText().toString();
String t = toughness.getText().toString();
String a = agility.getText().toString();
String i = intelligence.getText().toString();
String p = perception.getText().toString();
String wp = will_power.getText().toString();
String f = fellowship.getText().toString();
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Home_world, hw);
editor.putString(Weapon_skill, ws);
editor.putString(Ballistic_skill, bs);
editor.putString(Strength, s);
editor.putString(Toughness, t);
editor.putString(Agility, a);
editor.putString(Intelligence, i);
editor.putString(Perception, p);
editor.putString(Will_power, wp);
editor.putString(Fellowship, f);
editor.commit();
}
}
我已經使用的代碼來自: http://www.tutorialspoint.com/android/android_shared_preferences.htm
你是對的!非常感謝 – 2014-12-04 22:38:44