计算按钮被点击的次数

Counting the amount of times a button is clicked

我是 Android 的新手,我正在尝试创建一个可以保持比赛得分的应用程序。

我有 6 个按钮,每个按钮 3 个队,在橄榄球比赛中根据分数分配不同的增量值。通过这些按钮,我还希望能够跟踪每个按钮被点击的次数,并在 Toast 消息和电子邮件意图中 return 这些值。

我已经创建了 Toast 消息和电子邮件意图,我只需要用我已经声明为 countPenA 的按钮点击的计数来填充它们,等等。应用程序工作正常,就像这样问题。 我知道这对某些人来说可能很简单,但它打败了我。我想不通。

public class rugby_counter extends Activity implements View.OnClickListener {

EditText editTextA, editTextB;
TextView textViewA, textViewB ;
Button penA, conA, tryA, penB, conB, tryB, reSet;
int countA = 0;
int countB = 0;
int countPenA, countConA, countTryA, countPenB, countConB, countTryB = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rugby_counter);

    editTextA = (EditText) findViewById(R.id.editTextA);
    editTextB = (EditText) findViewById(R.id.editTextB);

    textViewA = (TextView) findViewById(R.id.textViewA);
    textViewB = (TextView) findViewById(R.id.textViewB);

    penA = (Button) findViewById(R.id.penBtnA);
    tryA = (Button) findViewById(R.id.tryBtnA);
    conA = (Button) findViewById(R.id.conBtnA);

    penB = (Button) findViewById(R.id.penBtnB);
    tryB = (Button) findViewById(R.id.tryBtnB);
    conB = (Button) findViewById(R.id.conBtnB);

    reSet = (Button) findViewById(R.id.restBtn);



    //---set on click listeners on the buttons-----
    penA.setOnClickListener(this);
    tryA.setOnClickListener(this);
    conA.setOnClickListener(this);
    penB.setOnClickListener(this);
    tryB.setOnClickListener(this);
    conB.setOnClickListener(this);
    reSet.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.penBtnA:
            countA += 3;
            textViewA.setText(Integer.toString(countA));

            break;

        case R.id.conBtnA:
            countA += 2;
            textViewA.setText(Integer.toString(countA));

            break;

        case R.id.tryBtnA:
            countA += 5;
            textViewA.setText(Integer.toString(countA));

            break;

        case R.id.penBtnB:
            countB += 3;
            textViewB.setText(Integer.toString(countB));

            break;

        case R.id.conBtnB:
            countB += 2;
            textViewB.setText(Integer.toString(countB));

            break;

        case R.id.tryBtnB:
            countB += 5;
            textViewB.setText(Integer.toString(countB));

            break;

        case R.id.restBtn:

            if (v == reSet) {
                countA = 0;
                countB = 0;
                textViewA.setText(Integer.toString(countA));
                textViewB.setText(Integer.toString(countB));
                editTextA.setText("");
                editTextB.setText("");
            }
    }


}


public void summary(View view) {




    String teamA = editTextA.getText().toString();
    String teamB = editTextB.getText().toString();
    String scoreA = textViewA.getText().toString();
    String scoreB = textViewB.getText().toString();




    if (teamA.equals("") || teamB.equals("") || scoreA.equals("") || scoreB.equals(""))
    {
        Toast noScore = Toast.makeText(this, getString(R.string.noScoreToast)
                , Toast.LENGTH_LONG);
        noScore.show();


    }


    else {
        Toast scores = Toast.makeText(this, "SCORE: \n" + teamA + ":" + scoreA + "\n" + teamB + ":" + scoreB +
                "\n \n" + "*****MATCH STATISTICS*****" +
                "\n \n" + teamA + ":" + "PENALTIES-" +  "CONVERSIONS-" + "TRIES- \n" + teamB + ":" + "PENALTIES-" + "CONVERSIONS-" + "TRIES-", Toast.LENGTH_LONG);
        scores.show();

    }
}


    public void email (View view)
    {

        String teamA = editTextA.getText().toString();
        String teamB = editTextB.getText().toString();
        String scoreA = textViewA.getText().toString();
        String scoreB = textViewB.getText().toString();

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Match Result");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "SCORE: \n" + teamA + ":" + scoreA + "\n" + teamB + ":" + scoreB +
                "\n \n" + "*****MATCH STATISTICS*****" +
                "\n \n" + teamA + ":" + "PENALTIES-"  + "CONVERSIONS-" +"TRIES- \n" + teamB + ":"+ "PENALTIES-"  + "CONVERSIONS-" +"TRIES-");


        startActivity(Intent.createChooser(emailIntent, getString(R.string.email_choose)));

    }
}

首先在开始时将 countPenA 和 countPenB 初始化为零,然后单击按钮 "A" 只需在按钮 "A" 的 onClickListener 代码中增加这样的值 :-

countPenA++;

现在,您可以使用此变量来获取用户在按钮 "A" 上进行的点击次数,同样可以对按钮 "B" 和您需要的任何其他按钮执行相同的操作。

使用 SharedPreferences 很简单:使用此代码计算按钮点击的总数
使用 SharedPrefereces,数据将被永久存储,即使用户退出你的应用程序`

SharedPreferences sharedPreferences;  //Declare Globally
SharedPreferences.Editor editor;      //Declare Globally
private static int Count = 0;         //use private static int globally

    sharedPreferences = getApplicationContext().getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);


    findViewById(R.id.example_button).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            /*
            * Using Shared Preferences to save data
              and at the same time update the count as the button is pressed
              multiple times               
           * */
            putValueInSharedPrefs(++Count);
        }
    });

public void putValueInSharedPrefs(int count)
{
    editor = sharedPreferences.edit();
    editor.putInt("DISMISS_BUTTON_CLICK_COUNT", count);
    editor.apply();

    Toast.makeText(Activity.this, "Example Button is clicked " +count+ "time(s)", Toast.LENGTH_SHORT).show();
}