2013-06-12 50 views
0

我有兩個EditTexts有兩個不同的值和一個按鈕,單擊該按鈕時,乘法結果應顯示在第三個Edit Text中。在按鈕上動態設置EditText中的文本點擊

編寫的代碼上按鈕的功能,請點擊:

public void Multiply (View view) 
{ 
String str1 = ed1.getText().toString(); 
     String str2 = ed2.getText().toString(); 

     int num1 = Integer.parseInt(str1); 
     int num2 = Integer.parseInt(str2); 

     int prod = num1*num2; 

     //Toast.makeText(getBaseContext(), "Product is"+(num1*num2),Toast.LENGTH_LONG).show(); 

     ed3.setText(prod); 

} 
+1

您設置int值,但Android expext整數==資源ID。所以你需要將prod轉換爲String。 ed3.setText(將String.valueOf(PROD)) – Borys

回答

2

錯誤:

ed3.setText(prod); 

嘗試:

ed3.setText(String.valueOf(prod)); 
0

的EditText不會讓整在setText直接進入。所以它轉換成字符串設置它

試試這個

ed3.setText(String.valueOf(prod));

+0

它顯示了一些語法錯誤,要求在表達式後插入')'。 – user1227670

+0

嗯..有線你有沒有試過這個'ed3.setText(String.valueOf(prod));' – GoCrazy

+0

我已經使用完全相同的表達式,但它顯示這個錯誤。 – user1227670

1

使用前

ed3.setText(String.valueOf(prod)); 

ed3.setText(""+prod); 
+1

哇...比爾蓋茨,你是天才。 –

0

你可以簡單的添加ed3.setText(""+prod);爲EditText上直接不接受整數值。

相關問題