请帮助创建代码来计算按钮点击的百分比
Please help create code to calculate the percent of clicks on the button
我是一名开发新手。请帮助创建代码来计算按钮的点击次数(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;
}
}
}
以下应该有所帮助
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)) + " %");
}
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)));
}
我是一名开发新手。请帮助创建代码来计算按钮的点击次数(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;
}
}
}
以下应该有所帮助
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)) + " %");
}
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)));
}