gpt4 book ai didi

Android Sharedpreferences 不会保存

转载 作者:行者123 更新时间:2023-11-30 02:31:19 24 4
gpt4 key购买 nike

我创建了一个代码,当我有一个名为“@+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

最佳答案

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

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

android:onClick="run"

关于Android Sharedpreferences 不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com