Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 如何定义计数器未通过重新启动活动重置?_Java_Android_Counter - Fatal编程技术网

Java 如何定义计数器未通过重新启动活动重置?

Java 如何定义计数器未通过重新启动活动重置?,java,android,counter,Java,Android,Counter,我的代码中定义了两个计数器 当将单个用户ID的正确答案添加到true_计数器时,如果将错误的材料添加到fales_计数器,则将错误答案添加到用户,但问题是当活动将计数器重新加载为零时 package com.Learning.math; import java.util.Random; import org.w3c.dom.Text; import com.Learning.math.R; import android.app.Activity; import android.conte

我的代码中定义了两个计数器

当将单个用户ID的正确答案添加到true_计数器时,如果将错误的材料添加到fales_计数器,则将错误答案添加到用户,但问题是当活动将计数器重新加载为零时

package com.Learning.math;

import java.util.Random;

import org.w3c.dom.Text;

import com.Learning.math.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Plus extends Activity {
private TextView num11;
private TextView num22;
public Integer no1;

public Integer no2;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plus_page);
    // Set Numbers To TextView's
    num11 = (TextView) findViewById(R.id.num1);
    num22 = (TextView) findViewById(R.id.num2);
    Intent iin = getIntent();
    Bundle b = iin.getExtras();
    if (b != null) {
        final String numb1 = (String) b.get("from");
        final String numb2 = (String) b.get("to");
        Integer min = Integer.valueOf(numb1);
        Integer max = Integer.valueOf(numb2);
        Random r1 = new Random();
        int random1 = r1.nextInt(max + -min) + min;
        Random r2 = new Random();
        int random2 = r2.nextInt(max - min) + min;
        String str1 = String.valueOf(random1);
        String str2 = String.valueOf(random2);
        num11.setText(str1 + "");
        num22.setText(str2 + "");
        no1 = Integer.valueOf(str1);
        no2 = Integer.valueOf(str2);
    }

    final EditText answerbox = (EditText) findViewById(R.id.answer_box);
    final ImageView true_pic = (ImageView) findViewById(R.id.imageView1);
    final ImageView false_pic = (ImageView) findViewById(R.id.imageView2);
    final Handler handler = new Handler();
    final TextView afarin = (TextView) findViewById(R.id.afarin_txt);
    final TextView try_again = (TextView) findViewById(R.id.try_again_txt);
    final TextView false_counter_txt = (TextView) findViewById(R.id.true_counter);
    final TextView true_counter_txt = (TextView) findViewById(R.id.false_counter);
    // تعریف فونت طناب
    Typeface tf = Typeface
            .createFromAsset(getAssets(), "fonts/tanab_0.ttf");
    afarin.setTypeface(tf);
    try_again.setTypeface(tf);
    // /////////////
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        private int true_counter;
        private int false_counter;

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String Answr = answerbox.getText().toString();
            Integer answer = Integer.valueOf(Answr);
            if (answer == (no2 + no1)) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Do something after 3s = 3000ms
                        startActivity(getIntent());
                    }
                }, 2000);

                hideSoftKeyboard(Plus.this);
                true_pic.setVisibility(View.VISIBLE);
                afarin.setVisibility(View.VISIBLE);
                true_counter++;
                true_counter_txt.setText(Integer.toString(true_counter));
            } else {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Do something after 3s = 3000ms
                        startActivity(getIntent());
                    }
                }, 2000);
                hideSoftKeyboard(Plus.this);
                false_pic.setVisibility(View.VISIBLE);
                try_again.setVisibility(View.VISIBLE);
                false_counter++;
                false_counter_txt.setText(Integer.toString(false_counter));
            }
        }
    });
}

// /////////// Hide Keyboard When Clicking
public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
            .getWindowToken(), 0);
}

}

实际上相当简单,只需将计数器
声明为
static

将变量声明为
static
,或者您也可以将该值存储在
SharedPreferences
中。

您需要重写
onSaveInstanceState()
方法来写入计数器值并使用
onCreate(Bundle)
读取计数器值的方法(使用前检查捆绑包为空)

使用
static
是不好的,因为如果你的应用程序在后台关闭,然后恢复,它就会消失


使用
SharedReferences
也可以,但它更适合长期存储(在应用程序的多次运行中保持),而实例状态则适合短期使用(在应用程序的一次运行中保持)。可以将其视为使用共享pref存储高分,使用实例状态存储当前游戏的数据。

您可以在SavedInstanceState上保存计数器值

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putInt("true_counter", true_counter_value);
  savedInstanceState.putInt("false_counter",false_counter_value);

}
您可以在创建时恢复计数器值,或在恢复安装状态时恢复计数器值

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putInt("true_counter", true_counter_value);
  savedInstanceState.putInt("false_counter",false_counter_value);

}

另一种方法是将计数器值保存到SharedReferences,您可以在此处找到有关保存和加载共享首选项数据的详细信息:

字段true\u计数器不能在非静态内部类型中声明为静态,除非使用常量表达式初始化