我希望我的应用程序执行的是计算平均值并将其显示在TextView中。
我有一个带有两个按钮(button0和button1)和六个TextView的布局。当我按下其中一个按钮时,我的应用将获得其被点击的点击次数的计数,而另一个按钮则得到相同的结果。并且还获得两个按钮被按下的总点击数。因此,如果我将按下的点击数按钮0除以按下的总点击数,然后将两个按钮相乘,再乘以100,就得出该按钮被按下的百分比。
所以这是代码:
Button button0, button1;
int click_button0, click_button1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
final TextView times_0_tv = (TextView) view.findViewById(R.id.times_0);
final TextView percentage_0_tv = (TextView) view.findViewById(R.id.percentage_0);
final TextView times_1_tv = (TextView) view.findViewById(R.id.times_1);
final TextView percentage_1_tv = (TextView) view.findViewById(R.id.percentage_1);
final TextView total_clicks_tv = (TextView) view.findViewById(R.id.total_clicks);
button0 = (Button) view.findViewById(R.id.button0);
button1 = (Button) view.findViewById(R.id.button1);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_button0 = click_button0 + 1;
total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
if (click_button0 == 1) {
Toast.makeText(getActivity(), "Number 0 has apperared 1 time", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Number 0 has apperared " + click_button0 + " times", Toast.LENGTH_SHORT).show();
}
times_0_tv.setText(String.valueOf(click_button0));
times_1_tv.setText(String.valueOf(click_button1));
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_button1 = click_button1 + 1;
total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
if (click_button1 == 1) {
Toast.makeText(getActivity(), "Number 1 has apperared 1 time", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Number 1 has apperared " + click_button1 + " times", Toast.LENGTH_SHORT).show();
}
times_0_tv.setText(String.valueOf(click_button0));
times_1_tv.setText(String.valueOf(click_button1));
}
});
times_0_tv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String times0String = times_0_tv.getText().toString();
String times1String = times_1_tv.getText().toString();
String totaltimesString = total_clicks_tv.getText().toString();
// convert the String into a double
if (times0String.length() > 0) {
click_button0 = (int) Double.parseDouble(times0String);
}
if (times1String.length() > 0) {
click_button1 = (int) Double.parseDouble(times1String);
}
if (totaltimesString.length() > 0) {
total_clicks = (int) Double.parseDouble(totaltimesString);
}
// calculate re
double percent0calc = calc_percent0();
// set the label for re1Text
percentage_0_tv.setText(Double.toString(percent0calc));
}
});
times_1_tv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String times0String = times_0_tv.getText().toString();
String times1String = times_1_tv.getText().toString();
String totaltimesString = total_clicks_tv.getText().toString();
// convert the String into a double
if (times0String.length() > 0) {
click_button0 = (int) Double.parseDouble(times0String);
}
if (times1String.length() > 0) {
click_button1 = (int) Double.parseDouble(times1String);
}
if (totaltimesString.length() > 0) {
total_clicks = (int) Double.parseDouble(totaltimesString);
}
// calculate re
double percent1calc = calc_percent1();
// set the label for re1Text
percentage_1_tv.setText(Double.toString(percent1calc));
}
});
}
double calc_percent0() {
return click_button0/total_clicks;
}
double calc_percent1() {
return click_button1/total_clicks;
}
这是布局的屏幕截图:
当我按下按钮时出现问题,百分比为0%,它没有变化。这个想法是,例如,如果我按下button0 3次和button1 1次,则0的百分比为75%,1的百分比为25%。任何想法都会受到欢迎,谢谢!
编辑:我删除了TextWatcher的实现,并且在代码中添加了几行。当我单击button0时,它工作正常,因为它显示单击的百分比为100%,但是当我单击button1时,两个百分比都变为0,而我没有任何百分比。将总点击次数的值传递到百分比计算时似乎出现了问题。
现在是片段的代码:
Button button0, button1;
int click_button0, click_button1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
final TextView times_0_tv = (TextView) view.findViewById(R.id.times_0);
final TextView percentage_0_tv = (TextView) view.findViewById(R.id.percentage_0);
final TextView times_1_tv = (TextView) view.findViewById(R.id.times_1);
final TextView percentage_1_tv = (TextView) view.findViewById(R.id.percentage_1);
final TextView total_clicks_tv = (TextView) view.findViewById(R.id.total_clicks);
button0 = (Button) view.findViewById(R.id.button0);
button1 = (Button) view.findViewById(R.id.button1);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_button0 = click_button0 + 1;
total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
if (click_button0 == 1) {
Toast.makeText(getActivity(), "Number 0 has apperared 1 time", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Number 0 has apperared " + click_button0 + " times", Toast.LENGTH_SHORT).show();
}
times_0_tv.setText(String.valueOf(click_button0));
times_1_tv.setText(String.valueOf(click_button1));
percent0 = click_button0/total_clicks;
percentage_0_tv.setText(String.valueOf(percent0));
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_button1 = click_button1 + 1;
total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
if (click_button1 == 1) {
Toast.makeText(getActivity(), "Number 1 has apperared 1 time", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Number 1 has apperared " + click_button1 + " times", Toast.LENGTH_SHORT).show();
}
times_0_tv.setText(String.valueOf(click_button0));
times_1_tv.setText(String.valueOf(click_button1));
percent1 = click_button1/total_clicks;
percentage_1_tv.setText(String.valueOf(percent1));
}
});
}
最佳答案
您为什么不只计算并设置onClick按钮中的百分比?您想在单击按钮时更新百分比,因此逻辑应该在按钮OnClickListener而非TextWatcher内。
另外,不要从TextViews中获取值,而要使用已有的变量:int click_button0, click_button1;
使用变量值在onClick中进行计算,然后设置TextViews值,它应该可以正常工作。
另外,如果要尝试将字符串解析为数字double,int等,则在出现异常的情况下应将其括在try catch中。在这种情况下,您应该只使用Integer.parseInt而不是使用parseDouble,然后将其强制转换为int。
编辑:是的,那是因为您将一个整数除以一个整数,所以结果也将是一个整数。一种快速的解决方法是将其中一个数字转换为浮点数/双精度数,除之后将得到一个十进制数。((double) click_button0) / total_clicks;
您还应该同时更新两个百分比,因为一个数字的变化会影响另一个数字。