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

时间:2015-07-12 09:02:06

标签: android android-fragments textview android-button textwatcher

我希望我的应用做的是计算平均值并将其显示在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;
}

以下是布局的屏幕截图:

enter image description here

按下按钮时出现问题,百分比为0%,不会改变。例如,如果我按下按钮0 3次并按下按钮1 1次,则0的百分比为75%,1的百分比为25%。任何想法都会受到欢迎,谢谢!

编辑:我删除了TextWatcher的实现,并在代码中添加了一些行。单击button0时它工作正常,因为它显示单击百分比为100%,但是当我单击button1时,两个百分比都变为0,我没有得到任何百分比。将总点击次数的值传递给百分比计算时似乎存在问题。

以下是Fragment的代码:

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));
        }
    });
}

1 个答案:

答案 0 :(得分:1)

为什么不在onClick按钮上计算和设置百分比?您希望在单击按钮时更新百分比,因此逻辑应位于OnClickListener按钮内,而不是TextWatcher。

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

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

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

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

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