【问题标题】:I need my counter to stop counting and display a toast when it reaches 510我需要我的计数器在达到 510 时停止计数并显示敬酒
【发布时间】:2016-08-18 09:07:54
【问题描述】:

这是目前的数学部分,我为每个文本视图元素设置了一个,它计算得很好,我需要的是所有文本视图中的数字在总数达到 510 时停止计数,并且所有其他人在达到 252 时停止计数

当总数达到 510 和/或单个值达到 252 时,我想要一个显示“已达到最高数字”或类似内容的吐司

如果有人不知道,这就是 Java,它在 android studio 中

private View.OnClickListener hpListener = new View.OnClickListener() {
public void onClick(View view) {

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.evval);
    // TextView element
    TextView HPtx = (TextView) findViewById(R.id.hpval);
    TextView atttx = (TextView) findViewById(R.id.attval);
    TextView deftx = (TextView) findViewById(R.id.defval);
    TextView satx = (TextView) findViewById(R.id.saval);
    TextView sdtx = (TextView) findViewById(R.id.sdval);
    TextView spdtx = (TextView) findViewById(R.id.spdval);
    TextView totaltx = (TextView) findViewById(R.id.totalval);

    // Ev values
    int add = Integer.parseInt(spinner.getSelectedItem().toString());

    // Calculation
    int HP = Integer.parseInt(HPtx.getText().toString());
    int att = Integer.parseInt(atttx.getText().toString());
    int def = Integer.parseInt(deftx.getText().toString());
    int sa = Integer.parseInt(satx.getText().toString());
    int sd = Integer.parseInt(sdtx.getText().toString());
    int spd = Integer.parseInt(spdtx.getText().toString());
    int hptotal = HP + add;
    int total = hptotal + att + def + sa + sd + spd;

    // Value calculations
    DecimalFormat addHPFormat = new DecimalFormat("000");
    DecimalFormat addtotalFormat = new DecimalFormat("000");
    HPtx.setText(addHPFormat.format(hptotal));
    totaltx.setText(addtotalFormat.format(total));
    }
};

【问题讨论】:

  • 欢迎来到 Stack Overflow!能否在您努力解决问题的同时,在内容中提供更好的标题和更详细的信息?

标签: java android textview listener


【解决方案1】:

只需将其放在您的代码中即可:

if(total >= 510 || hp >=252 || att >=252 || def >=252 || sa >=252 || sd >=252 || spd >=252)
    Toast.makeText(this,"highest Number Reached",Toast.LENGTH_SHORT).show();
else {
 DecimalFormat addHPFormat = new DecimalFormat("000");
    DecimalFormat addtotalFormat = new DecimalFormat("000");
    HPtx.setText(addHPFormat.format(hptotal));
    totaltx.setText(addtotalFormat.format(total));
}

【讨论】:

    【解决方案2】:
    private View.OnClickListener hpListener = new View.OnClickListener() {
    public void onClick(View view) {
    
        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.evval);
        // TextView element
        TextView HPtx = (TextView) findViewById(R.id.hpval);
        TextView atttx = (TextView) findViewById(R.id.attval);
        TextView deftx = (TextView) findViewById(R.id.defval);
        TextView satx = (TextView) findViewById(R.id.saval);
        TextView sdtx = (TextView) findViewById(R.id.sdval);
        TextView spdtx = (TextView) findViewById(R.id.spdval);
        TextView totaltx = (TextView) findViewById(R.id.totalval);
    
        // Ev values
        int add = Integer.parseInt(spinner.getSelectedItem().toString());
    
        // Calculation
        int HP = Integer.parseInt(HPtx.getText().toString());
        int att = Integer.parseInt(atttx.getText().toString());
        int def = Integer.parseInt(deftx.getText().toString());
        int sa = Integer.parseInt(satx.getText().toString());
        int sd = Integer.parseInt(sdtx.getText().toString());
        int spd = Integer.parseInt(spdtx.getText().toString());
        int hptotal = HP + add;
        int total = hptotal + att + def + sa + sd + spd;
    
        if (total >= 510 || add >= 253 || att >= 252 || def >= 252 || sa >= 252 || sf >= 252 || spd >= 252) {
             // Replace MainActivity.this with your context
             Toast.makeText(MainActivity.this, "Highest number reached", Toast.LENGTH_SHORT).show();
             return;
        }
    
        // Value calculations
        DecimalFormat addHPFormat = new DecimalFormat("000");
        DecimalFormat addtotalFormat = new DecimalFormat("000");
        HPtx.setText(addHPFormat.format(hptotal));
        totaltx.setText(addtotalFormat.format(total));
        }
    };
    

    【讨论】:

    • 这有点完美,唯一的问题是微调器每次最多可以将值增加 12,所以如果值到达,比如说 261,吐司会弹出,但我需要它在达到 253 之前停止计数
    • 这意味着您还想检查微调器 evval 吗?
    • 如果是,我为你修改了答案:)
    • 我觉得不对,你看到微调器的值没有改变,它看起来像这样:好的,所以我无法正确显示代码,但是微调器是一个列表,值为 1、4、8 和 12。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2023-03-12
    相关资源
    最近更新 更多