我希望将应用中的用户数据保存为一次性活动,这样它就不会再次询问了,我有这样的假设......
public class Main_A extends Activity {
public static final String Sh_Pref = "Sh_Pref_data";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s_pref);
SharedPreferences settings = getSharedPreferences(Sh_Pref, 0);
HashMap<String, String> map = (HashMap<String, String>) settings.getAll();
if (map != null && (map.containsKey("dtype") && map.containsKey("user_id") && map.containsKey("gname")&& map.get("dtype") != null && map.get("user_id") != null && map.get("gname") != null ))
{
Main_A.this.finish();
Intent ss = new Intent(Main_A.this, Splash.class);
ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ss);
}
else {
Button b = (Button) findViewById(R.id.save);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Spinner dtype = (Spinner) findViewById(R.id.s1);
EditText user_id = (EditText) findViewById(R.id.et1);
Spinner gname = (Spinner) findViewById(R.id.s2);
Spinner utype = (Spinner) findViewById(R.id.s3);
Spinner ttype = (Spinner) findViewById(R.id.s4);
if (dtype.getSelectedItem().toString().length() > 0 && user_id.getText().toString().length() > 0 && user_id.getText().toString().length() > 0 && utype.getSelectedItem().toString().length() > 0 && ttype.getSelectedItem().toString().length() > 0 && dtype.getSelectedItem().toString().length() > 0) {
String ct=dtype.getSelectedItem().toString();
String dom = user_id.getText().toString();
String am=gname.getSelectedItem().toString();
String ut=utype.getSelectedItem().toString();
String aname = getString(taname);
if(ut.equals("Talent"))
{
ut="T";
}
else if(ut.equals("A+"))
{
ut="A";
}
else if(ut.equals("Saint"))
{
ut="S";
}
String tt=ttype.getSelectedItem().toString();
Map<String, String> map = new HashMap<String, String>();
SharedPreferences settings = getSharedPreferences(Sh_Pref, 0);
SharedPreferences.Editor editor = settings.edit();
map.put("dtype",ct);
map.put("user_id", dom);
map.put("gname",am);
for (String key : map.keySet()) {
editor.putString(key, map.get(key));
}
editor.commit();
Main_A.this.finish();
Intent i = new Intent(Main_A.this, Splash.class);
startActivity(i);
}
}
});
Button c = (Button) findViewById(R.id.cancel);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main_A.this);
alertDialogBuilder.setTitle("ⓘ Exit ! " + getString(R.string.app_name));
alertDialogBuilder
.setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("ⓘ Exit ! " + getString(R.string.app_name));
alertDialogBuilder
.setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
此处保存所有数据并成功运行
这里我想在另一个Activity中编辑同一个文件..但是我需要查看以及之前的值是什么......
因为它是一次性活动......在那个应用程序启动之后......所以如果有什么不对的话只编辑那个特定的值......
由于错误的详细信息应用程序可能出现错误..对于此用户需要清除缓存/数据或卸载并重新安装..所以要避免这个我想添加编辑共享首选项..
任何人都可以建议我这样
答案 0 :(得分:1)
在此尝试此操作您可以编辑/更新/保存共享首选项
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private SharedPreferences prefs;
private String prefName = "report";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
Button save=(Button) findViewById(R.id.button1);
Button select=(Button) findViewById(R.id.button2);
Button update=(Button) findViewById(R.id.button3);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("id", Integer.parseInt(e_id.getText().toString()));
editor.putString("name", e_name.getText().toString());
//---saves the values---
editor.commit();
Toast.makeText(getBaseContext(), "Saved",
Toast.LENGTH_SHORT).show();
}
});
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
Toast.makeText(getBaseContext(), String.valueOf(prefs.getInt
("id", 22)), Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), prefs.getString
("name", "Sample Name"), Toast.LENGTH_SHORT).show();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("id", Integer.parseInt(e_id.getText().toString()));
editor.putString("name", e_name.getText().toString());
//---saves and update the values both are same---
editor.commit();
Toast.makeText(getBaseContext(), "Updated Successfully",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
在这里,您可以轻松更新现有值或清除退出值... 因此,您已完成保存在第二个活动中使用此...
答案 1 :(得分:0)
使用此行编辑您的共享首选项.. 放置要编辑的键。
settings.edit().putString(key, map.get(key)).apply();
答案 2 :(得分:0)
使用以下代码编辑共享首选项中特定键的值:
SharedPreferences.Editor editor = getSharedPreferences("Sh_Pref_data", MODE_PRIVATE).edit();
editor.putString("KEY",VALUE);
editor.commit();