【发布时间】:2015-07-12 09:02:06
【问题描述】:
我希望我的应用做的是计算平均值并将其显示在 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));
}
});
}
【问题讨论】:
标签: android android-fragments textview android-button textwatcher