-1

テキスト/数字の分割方法に問題があります。テキスト編集行の数字を分割したいのですが、このコードを持っていますが、私のやり方は間違っていますか? これが私のコード全体です:

    package com.trafika.rafa;

import java.util.Scanner;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button calculate = (Button) findViewById(R.id.calculateB);
        final TextView RM = (TextView) findViewById(R.id.returnMoney1);
        final EditText GM = (EditText) findViewById(R.id.gMoney);
        final EditText COP = (EditText) findViewById(R.id.cOP);

        calculate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    int givedMoney = Integer.parseInt(GM.getText().toString());
                    // String returnMoney = RM.getText().toString();
                    // String CostOfProduct = COP.getText().toString();
                    int CostOfProduct = Integer.parseInt(COP.getText().toString());
                    String parts[] = COP.getText().toString().split(" ");
                    for (int i = 0; i < parts.length; i++) {
                        CostOfProduct += Integer.parseInt(parts[i]);
                    }
                    int returnmoney;
                    returnmoney = givedMoney - CostOfProduct;
                    if (givedMoney < CostOfProduct) {
                        returnmoney = CostOfProduct - givedMoney;
                        RM.setTextColor(Color.RED);
                        RM.setText("Need more " + returnmoney + " den");
                        return;
                    }
                    RM.setTextColor(Color.WHITE);
                    RM.setText(returnmoney + " den");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

そして今、アプリケーションが正しく実行されていない場合、数値が正しく計算されず、混乱しています

4

2 に答える 2

1

発生しているエラーが何であるかわかりません。これを試しましたか:

COP.getText().toString().split("\\s+");

String.split() は、最初のパラメーターとして正規表現を取ります。この正規表現を使用すると、誰かが複数の空白文字を次々と使用する場合にも対処できます。

また、CostOfProduct を Integer.parseInt(COP.getText().toString()); で初期化する理由もわかりません。次に、同じ TextView の一部を使用して追加します。

代わりにこれを使用します:

                int CostOfProduct = 0;
                String parts[] = COP.getText().toString().split("\\s+");

より適切な実装では、getText() が null または空の文字列を返すことも考慮されます。

于 2013-07-06T19:09:41.117 に答える
0

例外ステートメントがないとわかりにくいですが、 の戻り値の型はそうでCOP.getText()はないjava.lang.Stringと思います (間違っていたら訂正してください。現時点ではテストできません)。ただし、コンパイルエラーが発生するはずです。それが問題であれば、COP.getText().toString().split(" ")うまくいくはずです。

于 2013-07-06T19:14:42.417 に答える