gpt4 book ai didi

java - 共享首选项 Android Studio

转载 作者:行者123 更新时间:2023-11-30 10:07:17 24 4
gpt4 key购买 nike

对于下面的代码,我试图检索共享首选项,我认为它已正确保存,但当我返回登录屏幕时,所有数据都消失了。当我返回此屏幕时,我需要它保留。所以我在个人资料页面上将姓名、年龄和 ID 输入到三个单独的行中。然后我按下保存按钮然后通过按操作栏上的后退转到之前的页面。当我返回个人资料页面时,我的信息应该仍然存在,但现在已经不存在了有帮助吗?

 package com.example.myprofile;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Savepoint;

public class Profile extends AppCompatActivity {

protected EditText NameEditText;
protected EditText AgeEditText;
protected EditText IDEditText;
protected Button saveButton;
protected Button settings_id;
String name;
String age;
String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);

EditText mEdit = (EditText) findViewById(R.id.NameEditText);
mEdit.setEnabled(false);
EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
mEdit1.setEnabled(false);
EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
mEdit2.setEnabled(false);

NameEditText = (EditText) findViewById(R.id.NameEditText);
AgeEditText = (EditText) findViewById(R.id.AgeEditText);
IDEditText = (EditText) findViewById(R.id.IDEditText);
settings_id = (Button) findViewById(R.id.settings_id);
saveButton = (Button) findViewById(R.id.SaveButton);



SharedPreferences prefs = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
name = prefs.getString("userName", "");
age = prefs.getString("userAge", "");
id = prefs.getString("userID", "");


saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


String name = NameEditText.getText().toString();
String age = AgeEditText.getText().toString();
String id = IDEditText.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.ProfileName), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.ProfileName), name);
editor.putString(getString(R.string.ProfileAge), age);
editor.putString(getString(R.string.ProfileID), id);
editor.apply();


if (Integer.parseInt(age) < 18)
{
Toast toast1 = Toast.makeText(getApplicationContext(), "Invalid Age", Toast.LENGTH_LONG);
toast1.show();
}
else if (!name.isEmpty() && !age.isEmpty() && !id.isEmpty())
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Toast toast = Toast.makeText(getApplicationContext(), "Name Saved!", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast2 = Toast.makeText(getApplicationContext(), "Incomplete Info", Toast.LENGTH_LONG);
toast2.show();
}


}
});

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings_id:
{
EditText mEdit = (EditText) findViewById(R.id.NameEditText);
mEdit.setEnabled(true);
EditText mEdit1 = (EditText) findViewById(R.id.AgeEditText);
mEdit1.setEnabled(true);
EditText mEdit2 = (EditText) findViewById(R.id.IDEditText);
mEdit2.setEnabled(true);
saveButton.setEnabled(Boolean.parseBoolean("True"));
}
default:
return super.onOptionsItemSelected(item);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;

}
}

最佳答案

For save

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("keyname",true);
editor.putString("keyname","string value");
editor.putInt("keyname","int value");
editor.putFloat("keyname","float value");
editor.putLong("keyname","long value");
editor.commit();

For get

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
pref.getString("keyname",null);
pref.getInt("keyname",0);
pref.getFloat("keyname",0);
pref.getBoolean("keyname",true);
pref.getLong("keyname",0);

For single delete

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove("keyname");
editor.commit();

For all delete

SharedPreferences pref = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();

关于java - 共享首选项 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394215/

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