我是Android开发的初学者,我遇到了保存更改的问题,某些文本显示在某些TextView中。但对我来说更难一点。 为了解释简单,我有2个活动,Activity1是主要的,Activity2是用户信息。 在Activity1(Main)中,用户clic按钮("登录")然后他继续Activity2,用户可以输入他的个人信息,然后点击按钮("保存数据")然后我在数据库(SQLite)上添加他的数据,然后当用户点击另一个按钮(" go")时,他返回Activity1我使用一个意图,以便在Activity1中的TextView上显示他的昵称a"欢迎" +昵称。 但是当用户退出应用程序时,没有保存,更改没有使用昵称保存,但昵称始终在我的数据库中。
我会告诉你一些代码: 活性2:
public class ProfilActivity extends Activity {
EditText editNickname; // We just work on the nickname so I just keep it
MyDatabase myDb;
Button bAddData, go;
public SharedPreferences prefs;
private String nicktobesaved;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_profil);
myDb = new MyDatabase(this);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
editNickname = (EditText) findViewById(R.id.pseudo2);
bAddData = (Button) findViewById(R.id.ajouter);
nicktobesaved = prefs.getString("nickname", "");
bAddData.setEnabled(prefs.getBoolean("isEnabled", true) ? true : false);
AddData();
} // End onCreate()
public void changeNickname(View v) {
// I delete this method who passed an intent
}
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nicktobesaved = prefs.getString("nickname", "");
nicktobesaved = editPseudo.getText().toString();
// The following code is just for save data on my database SQLite
boolean isInserted = myDb.insertData(editNickname.getText().toString());
if (isInserted == true) {
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).apply();
bAddData.setEnabled(false);
} else {
Toast.makeText(ProfilActivity.this, "Data not saved", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
活动1(主要活动):
public class MainActivity extends Activity {
TextView tv;
Button login;
public SharedPreferences prefs;
private String nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
nickname = prefs.getString("nickname", "");
login = (Button) findViewById(R.id.connexion);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentLogin = new Intent(MainActivity.this, ProfilActivity.class);
startActivity(intentLogin);
}
});
tv = (TextView)findViewById(R.id.pseudo);
if(nickname.equals("")) {
tv.setText("Welcome");
} else {
tv.setText("Welcome " + nickname);
}
}
}
Pseudo =昵称法文, Bienvenue =欢迎法语, 感谢大家的帮助,我希望你能理解我的问题。 Pikkoro
答案 0 :(得分:1)
我建议您使用Sharedpreferences存储基本信息,并阅读有关如何在Android上保存数据的一些教程或文档。这里我使用Sharedpreferences做了一个例子。希望它有所帮助!
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by Hasan on 4.05.2017.
*/
public class ProfilActivity extends Activity {
EditText editName, editNickname;
Button bAddData;
public SharedPreferences prefs;
//To be saved elements in sharedpreferences!!!
private String nicktobesaved, nametobesaved;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_profil);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
editName = (EditText) findViewById(R.id.name);
editNickname = (EditText) findViewById(R.id.pseudo2);
bAddData = (Button) findViewById(R.id.ajouter);
//default values
nametobesaved=prefs.getString("name", "");
nicktobesaved=prefs.getString("nickname", "");
bAddData.setEnabled(prefs.getBoolean("isEnabled", true) ? true : false);
if(!nametobesaved.equals("")) {
editName.setText(nametobesaved);
}
if (!nicktobesaved.equals("")){
editNickname.setText(nicktobesaved);
}
AddData();
} // End onCreate()
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nametobesaved=prefs.getString("name", "");
nicktobesaved=prefs.getString("nickname", "");
nametobesaved= editName.getText().toString();
nicktobesaved= editNickname.getText().toString();
if(nametobesaved.equals("") || nicktobesaved.equals("")){
Toast.makeText(ProfilActivity.this, "Fill all spaces", Toast.LENGTH_LONG).show();
}else {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", nametobesaved);
editor.putString("nickname", nicktobesaved);
editor.commit();
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).commit();
bAddData.setEnabled(false);
}
}
}
);
}
}
将昵称保存到Sharedpreferences后,您需要在主要活动中加载它。它是这样的:
public class MainActivity extends Activity {
public static final int PROFIL_REQ = 1;
public static final String PSEUDO="nickname";
TextView tv;
Button login;
//Sharedpreferences and nick name variables
private SharedPreferences prefs;
private String nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.pseudo);
//Load or create sharedpreferences
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
nickname = prefs.getString("nickname", "");
if(nickname.equals("")) {
tv.setText("Welcome");
}else{
tv.setText("Welcome "+ nickname);
}
}
那是因为您删除了共享偏好编辑器。
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nicktobesaved = prefs.getString("nickname", "");
nicktobesaved = editPseudo.getText().toString();
//DO NOT DELETE HERE
SharedPreferences.Editor editor = prefs.edit();
editor.putString("nickname", nicktobesaved);
editor.commit();
// The following code is just for save data on my database SQLite
boolean isInserted = myDb.insertData(editNickname.getText().toString());
if (isInserted == true) {
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).apply();
bAddData.setEnabled(false);
} else {
Toast.makeText(ProfilActivity.this, "Data not saved", Toast.LENGTH_LONG).show();
}
}
}
);
}