テキスト/数字の分割方法に問題があります。テキスト編集行の数字を分割したいのですが、このコードを持っていますが、私のやり方は間違っていますか? これが私のコード全体です:
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;
}
}
そして今、アプリケーションが正しく実行されていない場合、数値が正しく計算されず、混乱しています