gpt4 book ai didi

android - 离开 Activity 后保留变量

转载 作者:行者123 更新时间:2023-11-29 21:08:31 27 4
gpt4 key购买 nike

我正在尝试找到一种方法,让我的变量在应用程序的整个生命周期中保持不变。我设计该应用程序的方式是用户单击类(class)并提交成绩。我想要的是应用程序在 Activity 结束后存储成绩。当用户退出屏幕并返回时,他必须重新输入他得到的成绩。

我的页面代码如下:

选课 Activity

public class ClassCore extends Activity {

TextView eng101, eng102, mat186, com173;
TextView eng101scr, eng102scr, mat186scr, com173scr;
LinearLayout eng101bg, eng102bg, mat186bg, com173bg;
Button coreReturn;
int ss;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.classcore);

eng101bg = (LinearLayout)findViewById(R.id.eng101bg);
eng102bg = (LinearLayout)findViewById(R.id.eng102bg);
mat186bg = (LinearLayout)findViewById(R.id.mat186bg);
com173bg = (LinearLayout)findViewById(R.id.com173bg);

eng101 = (TextView)findViewById(R.id.eng101);
eng102 = (TextView)findViewById(R.id.eng102);
mat186 = (TextView)findViewById(R.id.mat186);
com173 = (TextView)findViewById(R.id.com173);

eng101scr = (TextView)findViewById(R.id.eng101CoreScr);
eng102scr = (TextView)findViewById(R.id.eng102CoreScr);
mat186scr = (TextView)findViewById(R.id.mat186CoreScr);
com173scr = (TextView)findViewById(R.id.com173CoreScr);

coreReturn = (Button)findViewById(R.id.btnCoreRtrn);


eng101.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(ClassCore.this, Eng101.class);
i.putExtra("grades", "eng101" );
startActivityForResult(i, 1);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("e101FinalScore")){
eng101scr.setText(data.getStringExtra("e101FinalScore"));
eng101bg.setBackgroundColor(Color.parseColor("#00FF00"));
}}

英语 Activity

public class Eng101 extends Activity {

Button btnSubmit;
EditText userGrade;
String strGrade;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eng101);

btnSubmit = (Button)findViewById(R.id.btnE101);
userGrade = (EditText)findViewById(R.id.eng101Scr);

btnSubmit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
strGrade = userGrade.getText().toString();
Intent i = getIntent();
String msg = i.getStringExtra("grades");
if(msg.contentEquals("eng101")){
i.putExtra("e101FinalScore", strGrade);
setResult(RESULT_OK, i);
finish();
}
}
});
}
}

最佳答案

您可以通过 3 种方式实现您的功能。

<强>1。全局变量

您可以通过使用修饰符 public static 定义任何变量来使用全局变量,例如

public static String GRADE = "10";

您可以通过 CLASS_NAME.GRADE 在应用中的任何类(class)中使用它。喜欢

String grade = CLASS_NAME.GRADE;

注意:当您关闭应用程序时,它会丢失其值。

<强>2。共享偏好

要在应用关闭后保留值,您需要使用 SharedPreference 或 SQLite 数据库。

这里是如何使用 SharedPreference 的例子.

在您的 Activity 中如下初始化 SharedPreference

SharedPreferences sp = getSharedPreferences("app_name", Context.MODE_PRIVATE);

如下所示从 SharedPreference 获取值

String grade = sp.getString("grade", "0");

如下设置SharedPreference的值

sp.edit().putString("grade", "5").commit();

<强>3。 SQLite

您可以使用 SQLite数据库来存储自己的结构数据。您可以根据您的要求创建不同的表并存储/检索日期。以下是一些示例:http://www.vogella.com/tutorials/AndroidSQLite/article.html

关于android - 离开 Activity 后保留变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707300/

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