0

我使用这个问题的答案将 TextView 点击的基本值转换为单个 TextView 。这段代码给出了每次点击的值,我想知道如何添加 TextView 的现有值。注意:m1aa 和 m1ab 是先前活动中的数字变量,并通过本次活动的意图接收。

更新这个问题应该是“如何将字符串转换为小数”。

    //Original non working code.
    private int productTotal1;
    private int productTotal2;
    private int overallTotalproduct;


    private TextView m1a, 
    private TextView m1aa, 

    //and then way down the page

    productTotal1 = 0;
    productTotal2 = 0;
    overallTotalproduct = 0;

    final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
    final TextView textView1 = (TextView) findViewById(R.id.m1aa);
    final TextView textView2 = (TextView) findViewById(R.id.m1ab);

    textView1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            productTotal1++;
            overallTotalproduct = productTotal1 + productTotal2;
            textView1.setText(String.valueOf(productTotal1));
            textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
        }
    });

    textView2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            productTotal2++;
            overallTotalproduct = productTotal1 + productTotal2;
            textView2.setText(String.valueOf(productTotal2));
            textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
        }
    });

***更新***工作正常 :) 在获得帮助和“思考”我所写的内容之后,我最终得到了……

//Working version
private double overallTotalproduct;



    final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);

    final TextView textView1 = (TextView) findViewById(R.id.m1aa);
    final String stringm1aa = textView1.getText().toString();
    final double intm1aa = Double.parseDouble(stringm1aa);


    final TextView textView2 = (TextView) findViewById(R.id.m1ab);
    final String stringm1ab = textView2.getText().toString();
    final double intm1ab = Double.parseDouble(stringm1ab);
4

1 回答 1

0

您可以使用以下代码获取值TextView并将其转换为整数

String s = textView.getText().toString();
int n = Integer.parseInt(s);

NumberFormatException如果字符串不包含可解析的整数,请记住将被抛出。

于 2013-06-02T15:41:11.513 回答