我需要使用 for 循环和数组来获取两个输入并减去它们(输入格式hh:mm:ss
)。然后在editText中输出差异。但我似乎无法让我的代码运行。
对不起,如果这是一个非常基本的问题。我花了几天时间在网上寻找试图理解这个问题。这是我第一次尝试 Java。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
end = (EditText) findViewById(R.id.etEnd);
start = (EditText) findViewById(R.id.etStart);
diff = (EditText) findViewById(R.id.etDiff);
calc = (Button) findViewById(R.id.bCalc);
clear = (Button) findViewById(R.id.bClear);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int hh = tot[0];
int mm = tot[1];
int ss = tot[2];
String sGet2 = end.getText().toString(); // end to string
String sGet1 = start.getText().toString(); // start to string
String[] erA = sGet2.split(":"); // end string to end array
String[] srA = sGet1.split(":"); // start string to string array
for (int i = 0; i < srA.length; i++) {
inted = Integer.parseInt(erA[i].trim());
intst = Integer.parseInt(srA[i].trim());
tot[i] = inted - intst;
if (i == 2) {
String mt = ":" + mm;
String st = ":" + ss;
String ht = ":" + hh;
String tota = mt + st;
String total = tota + ht;
out = String.format("%4.4s", total);
diff.setText(out);
} else
return;