【问题标题】:Counting the amount of times a button is clicked计算按钮被点击的次数
【发布时间】:2015-12-08 14:03:39
【问题描述】:

我对 Android 还很陌生,我正在尝试创建一个可以保持比赛得分的应用程序。

我有 6 个按钮,每个团队 3 个,它们被分配了不同的增量值在橄榄球比赛中得分。使用这些按钮,我还希望能够跟踪每个按钮被单击的次数,并在 Toast 消息和电子邮件意图中返回这些值。

我已经创建了 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)));

    }
}

【问题讨论】:

  • 您遇到了什么问题?我无法从问题中看出这一点。
  • 我需要统计按钮点击次数并在 toast 消息中显示计数。

标签: android android-intent android-button


【解决方案1】:

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

countPenA++;

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

【讨论】:

    【解决方案2】:

    使用 SharedPreferences 很简单:使用此代码计算按钮点击的总次数
    使用 SharedPreferences,即使用户退出您的应用,数据也会永久存储`

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 2014-12-06
      • 2017-11-29
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多