Android Sharedpreferences不会保存

时间:2014-12-04 22:22:16

标签: android save sharedpreferences android-imagebutton

我创建了一个代码,当我有一个名为“@ + id / buttonSave_character”的常规按钮时,该代码有效 但是,当我删除它并添加一个图像按钮并尝试保存我已存储在edittext中的值时,它不会这样做。代码出了什么问题?

另一件奇怪的事情是我可以看到我没有在代码中定义按钮但它仍然会保存它(使用旧的常规按钮)。

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

1 个答案:

答案 0 :(得分:1)

也许您错过了xml中的行来设置点击处理程序。

确保您的图片按钮的android:onClick属性设置为run,如下所示:

android:onClick="run"