【问题标题】:Get value of TextView from Button onClick and pass value to TextView via TextWatcher从 Button onClick 获取 TextView 的值并通过 TextWatcher 将值传递给 TextView
【发布时间】: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


    【解决方案1】:

    您为什么不直接计算并设置按钮 onClick 中的百分比?您想在单击按钮时更新百分比,因此逻辑应该在按钮 OnClickListener 内,而不是 TextWatcher。

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

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

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

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

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

    【讨论】:

    • 谢谢你的回答,真的很有用。我已经根据您的建议编辑了我的问题,但问题仍然存在。希望你有什么想法,谢谢!
    • 我已经编辑了我的答案以帮助您进行编辑,希望对您有所帮助。
    • 非常感谢,在您的帮助下终于搞定了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多