首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Sharedpreferences不会保存

Android Sharedpreferences不会保存
EN

Stack Overflow用户
提问于 2014-12-04 22:22:16
回答 1查看 69关注 0票数 0

我已经创建了一个代码,当我有一个名为“@+id/按钮保存字符”的常规按钮时,但是,当我删除它并添加一个图像按钮并尝试保存我在编辑文本中存储的值时,它不会这样做。密码有什么问题吗?

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

代码语言:javascript
运行
复制
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(); 

   }

}

我使用的代码来自:preferences.htm

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-04 22:26:09

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

确保图像按钮将android:onClick属性设置为run,如下所示:

代码语言:javascript
运行
复制
android:onClick="run"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27305293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档