计算单击按钮的次数

问题描述 投票:-1回答:2

我对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)));

    }
}
android android-intent android-button
2个回答
1
投票

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

countPenA++;

现在,您可以使用此变量来获取用户在按钮“A”上所做的点击次数,同样对按钮“B”和您需要的任何其他按钮也是如此。


0
投票

使用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();
}
© www.soinside.com 2019 - 2024. All rights reserved.