Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果我终止活动,textview中的文本将不保存?_Java_Android - Fatal编程技术网

Java 如果我终止活动,textview中的文本将不保存?

Java 如果我终止活动,textview中的文本将不保存?,java,android,Java,Android,我是android的新手,我创建了一个小应用程序,可以计算我上课的天数和我不上课的天数。但一旦我关闭应用程序并在textview中重新打开文本,就会返回默认值 import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import

我是android的新手,我创建了一个小应用程序,可以计算我上课的天数和我不上课的天数。但一旦我关闭应用程序并在textview中重新打开文本,就会返回默认值

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
Button b1,b2,b3;
TextView tv1,tv2,tv3;
int i=0;
int j=0;
int nd=0;
Products products;
MyDBHandler myDBHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDBHandler = new MyDBHandler(MainActivity.this,null,null,1);
    products = new Products("Present:"+i,"Absent:" + j,"Percentage" + nd +       "%");
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.calculate);
    tv1=(TextView)findViewById(R.id.textView);
    tv2=(TextView)findViewById(R.id.textView3);
    tv3=(TextView)findViewById(R.id.percentage);
    products.set_present("Present:"+i);
    products.set_absent("Absent:" + j);
    products.set_percentage("Percentage" + nd + "%");
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            i++;
            tv1.setText("Present:"+i);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            j++;
            tv2.setText("Absent:"+j);
        }
    });
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    int dmator=i+j;
    nd=(i*100)/dmator;
    tv3.setText("Percentage:"+nd+"%");

   if(nd>65)
        Toast.makeText(MainActivity.this,"Chill you are     safe",Toast.LENGTH_LONG).show();
    if(nd<65)
        Toast.makeText(MainActivity.this,"Attention youre attendance is less     than 65",Toast.LENGTH_LONG).show();
}
});


}


}`//three buttons

//three text views
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了ActionBarActivity{
按钮b1、b2、b3;
文本视图tv1、tv2、tv3;
int i=0;
int j=0;
int-nd=0;
产品;
MyDBHandler MyDBHandler;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDBHandler=新的myDBHandler(MainActivity.this,null,null,1);
产品=新产品(“存在:+i”,不存在:+j,“百分比”+nd+“%”);
b1=(按钮)findViewById(R.id.Button);
b2=(按钮)findViewById(R.id.button2);
b3=(按钮)findViewById(R.id.calculate);
tv1=(TextView)findViewById(R.id.TextView);
tv2=(TextView)findViewById(R.id.textView3);
tv3=(TextView)findViewById(R.id.百分比);
产品。当前设置(“当前:+i”);
产品。不存在集_(“不存在:+j”);
产品。设置百分比(“百分比”+nd+“%”);
b1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
i++;
tv1.setText(“当前:+i”);
}
});
b2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
j++;
tv2.setText(“缺席:+j”);
}
});
b3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int dmator=i+j;
nd=(i*100)/dmator;
tv3.setText(“百分比:+nd+”%);
如果(nd>65)
Toast.makeText(MainActivity.this,“Chill you safe”,Toast.LENGTH_LONG.show();

如果(nd如果在应用程序关闭后需要存储数据,则需要使用SharedReferences


发生这种情况的原因是活动被终止,因此,当再次创建活动时,所有对象都会再次初始化。您可以将此数据缓存在SharedReferences中,或者缓存在数据库中。您可以使用首选项保存数据。检查应用程序是否是第一次运行,或者保存一个boo,其中会检查上次或以后是否输入了任何数据不是。不管你喜欢哪种方式。下面是一个简短的偏好类,我可以想出

public class PreferenceValues {

private boolean isFirstRun;


private String daysPresent;
private String daysAbsent;
private String percentage;


private Editor ed;


private static PreferenceValues mInstance = null;


private static SharedPreferences prefs;

private PreferenceValues(Context context) {
    super();
}

/**
 * Creating a singleton insatnce of the class
 * 
 * @param ctx
 * @return instance of the class
 */
public static PreferenceValues getInstance() {
    Context ctx = MyApplication.getInstance().getApplicationContext();
    if (mInstance == null) {
        mInstance = new PreferenceValues(ctx);
    }
    prefs = ctx
            .getSharedPreferences("MyPreferencess", Context.MODE_PRIVATE);

    return mInstance;
}

/**
 * To check if the application is running for the first time
 * 
 * @return
 */
public boolean isFirstRun() {
    return prefs.getBoolean("isfirstruns", true);

}

/**
 * To update the flag for the first run
 * 
 * @param isFirstRun
 */
public void setIsFirstRun(Boolean isFirstRun) {
    this.isFirstRun = isFirstRun;
    ed = prefs.edit();
    ed.putBoolean("isfirstruns", this.isFirstRun);
    ed.commit();
}


public String getDaysPresent() {
    return prefs.getString("daysPresent", "0");
}

public void setDaysPresent(String daysPresent) {
    this.daysPresent = daysPresent;
    ed = prefs.edit();
    ed.putString("daysPresent", this.daysPresent);
    ed.commit();
}

public String getdaysAbsent() {
    return prefs.getString("daysAbsent", "0");
}

public void setdaysAbsent(String daysAbsent) {
    this.daysAbsent = daysAbsent;
    ed = prefs.edit();
    ed.putString("daysAbsent", this.daysAbsent);
    ed.commit();
}

public String getpercentage() {
    return prefs.getString("percentage", "0.00");
}

public void setpercentage(String percentage) {
    this.percentage = percentage;
    ed = prefs.edit();
    ed.putString("percentage", this.percentage);
    ed.commit();

}

}
以活动代码为例

 public class MainActivity extends ActionBarActivity {

Button btn;
EditText t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button1);
    t1 = (EditText) findViewById(R.id.textView1);

    t1.setText(PreferenceValues.getInstance(getApplicationContext()).getDaysPresent());

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            PreferenceValues.getInstance(getApplicationContext()).setDaysPresent(t1.getText().toString());

        }
    });
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    PreferenceValues.getInstance(getApplicationContext()).setDaysPresent(t1.getText().toString());
}

}

你必须保留数据。看看
SharedReferences
我很高兴你提出了一些有用的东西。我知道我应该使用SharedReferences,但我不知道如何在活动开始时立即提供输入和输出。我将阅读所有教程。谢谢!在活动开始时使用以下行当您的活动开始时,如果(PreferenceValues.getContext().getDaysPresent().equals(“0”){t1.setText(“”)}或者{t1.setText(PreferenceValues.getContext().getDaysPresent())}这将做的是..它将检查最后一个值是否为0,如果不是,它将显示上次保存的值。要保存按钮上的值,请单击使用PreferenceValue.getContext.setDaysPresent(“您需要保存的值”)。此外,您还可以在按下back btn或onPause()时保存相同的值如果您没有点击任何btn并使用back或home btn退出应用程序,我理解。最好的解释。谢谢,这是一个很好的教程。