【问题标题】:Please help create code to calculate the percent of clicks on the button请帮助创建代码来计算按钮点击的百分比
【发布时间】:2015-05-25 09:46:27
【问题描述】:

我是一名新手开发人员。请帮助创建代码以计算按钮的点击次数(在 Android 上)。我有三个 ImageButton(IB1、IB2、IB3)和 3 个空的 TextView(TV1、TV2、TV3)。但是,计数器不应考虑三个按钮中每个按钮的点击次数。计数器应被视为每个按钮的点击次数占此会话中所有三个按钮的总点击次数的百分比。这是我的代码的开头。在此先感谢大家。

public class ring_fight extends Activity implements View.OnClickListener {  



    ImageButton IB1, IB2, IB3;
    TextView TV1, TV2, TV3;

    // I declare counters
        int Count_for_IB1 = 0; //counter for ImageButton1 
        int Count_for_IB2 = 0; //counter for ImageButton2
        int Count_for_IB3 = 0;//counter for ImageButton3


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

        IB1 = (ImageButton) findViewById(R.id.IB_first);
        IB2 = (ImageButton) findViewById(R.id.IB_second);
        IB3 = (ImageButton) findViewById(R.id.IB_third);

        TV1 = (TextView)findViewById(R.id.TextView1);
        TV2 = (TextView)findViewById(R.id.TextView2);
        TV3 = (TextView)findViewById(R.id.TextView3);


        IB1.setOnClickListener(this);
        IB2.setOnClickListener(this);
        IB3.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.IB1:
                TV1.setText("");
                break;
            case R.id.IB2:
                TV1.setText("");
                break;
            case R.id.IB3:
                TV1.setText("");
                break;
        }
    }
}

【问题讨论】:

    标签: android click logic imagebutton


    【解决方案1】:
    float count1=0, count2=0, count3=0, total=0;
    public void onClick(View v) {
        switch (v.getId()) {
              case R.id.IB1:
                   count1++;
              break;
              case R.id.IB2:
                   count2++;
              break;
              case R.id.IB3:
                   count3++;
              break;
       }
       total=count1+count2+count3;
       TV1.setText(String.valueOf(((count1/total)/100)));
       TV2.setText(String.valueOf(((count2/total)/100)));
       TV3.setText(String.valueOf(((count3/total)/100)));
    }
    

    【讨论】:

    • 如何用单号限制小数位数,比如33.6,但是33.666666?
    • 谢谢,最后几行应该是这样的:TV3.setText(String.valueOf(((count3/total)*100)));
    【解决方案2】:

    下面应该有帮助

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.IB1:
                Count_for_IB1++;
                break;
            case R.id.IB2:
                Count_for_IB2++;
                break;
            case R.id.IB3:
                Count_for_IB3++;
                break;
        }
        TV1.setText((Count_for_IB1*100 / (Count_for_IB1 + Count_for_IB2 + Count_for_IB3)) + " %");
        TV2.setText((Count_for_IB2*100 / (Count_for_IB1 + Count_for_IB2 + Count_for_IB3)) + " %");
        TV3.setText((Count_for_IB3*100 / (Count_for_IB1 + Count_for_IB2 + Count_for_IB3)) + " %");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多