我需要使用for循環和數組來獲取兩個輸入並將它們相減(輸入格式hh:mm:ss
)。然後在editText中輸出差異。但我似乎無法讓我的代碼運行。使用for循環找到兩個時間字符串之間的差異(hh:mm:ss)
對不起,如果這是一個非常基本的問題。我花了好幾天的時間在網絡上試圖瞭解問題。這是我第一次嘗試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;
我應該拿出if和else語句,並把是否碼後的for循環?哦,失去了回報; – 2012-04-07 03:23:39
你究竟在做什麼 – 2012-04-07 03:25:49
我有三個EditText的。其中兩個用於輸入(例如hh:mm:ss)。第三個將在提交按鈕被按下時顯示差異。 – 2012-04-07 21:54:06