从 Button onClick 获取 TextView 的值并通过 TextWatcher 将值传递给 TextView

Get value of TextView from Button onClick and pass value to TextView via TextWatcher

我希望我的应用程序执行的操作是计算平均值并将其显示在 TextView 中。

我的布局有两个按钮(button0 和 button1)和六个 TextView。当我按下其中一个按钮时,我的应用程序会计算它被按下的点击次数,另一个按钮也是如此。它还会计算按下两个按钮的总点击次数。因此,如果我将 button0 被按下的点击次数除以这两个按钮被按下的总点击次数,然后乘以 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 中。

此外,不要从您的 TextView 获取值,而是使用您已有的变量:int click_button0, click_button1;

使用变量值在 onClick 中进行计算,然后设置 TextViews 值,它应该会按预期工作。

此外,如果您要尝试将字符串解析为数字 double、int 等,您应该将其包围在 try catch 中以防出现异常。在这种情况下,您应该只使用 Integer.parseInt 而不是使用 parseDouble 然后将其转换为 int.

编辑: 是的,那是因为您将一个整数除以一个整数,所以结果也将是一个整数。一个快速的解决方法是将其中一个数字转换为 float/double,当你除法时你会得到一个十进制数。 ((double) click_button0) / total_clicks;

您还应该同时更新这两个百分比,因为一个数字的变化会影响另一个。