2015-11-14 88 views
1

我是一個初學者的android應用程序開發人員。目前正試圖構建一個簡單的計算器應用程序,並希望通過使用Toast顯示計算結果。但現在我無法使用吐司來顯示這個輸出。還有就是我的代碼吐司不工作在我的Android應用程序

MainActivity.java

package com.example.calculator; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.app.Activity; 

public class MainActivity extends Activity { 
    EditText input_field1,input_field2,result_field; 
    int result_var; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void add_numbers(View v){ 

     input_field1= (EditText) findViewById(R.id.field1); 
     String no1=input_field1.getText().toString(); 
     input_field2= (EditText) findViewById(R.id.field2); 
     String no2=input_field2.getText().toString(); 
     int field1_int=Integer.parseInt(no1); 
     int field2_int=Integer.parseInt(no2); 
     result_field= (EditText) findViewById(R.id.resultfield); 
     result_var=field1_int+field2_int; 
     result_field.setText("result "+result_var); 
     Toast.makeText(getApplicationContext(), result_var, Toast.LENGTH_LONG).show(); 

    } 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_toRightOf="@+id/button1" 
     android:text="Substract" /> 

    <Button 
     android:id="@+id/divide" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button2" 
     android:layout_alignBottom="@+id/button2" 
     android:layout_toRightOf="@+id/button2" 
     android:text="Divide" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="105dp" 
     android:text="Add" 
     android:onClick="add_numbers" /> 


    <EditText 
     android:id="@+id/field1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="22dp" 
     android:ems="10" /> 

    <EditText 
     android:id="@+id/resultfield" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button2" 
     android:layout_marginTop="16dp" 
     android:ems="10" /> 

    <EditText 
     android:id="@+id/field2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button2" 
     android:layout_alignLeft="@+id/button1" 
     android:ems="10" /> 

</RelativeLayout> 
+0

你有一個logcat的錯誤?如果是這樣,請包括它。 – Tigger

+0

做了'result_field.setText(「result」+ result_var);'工作? – Eoin

+0

'editText1'未定義 – Eoin

回答

0

result_var應該是一個字符串

String result_var=""; 
String Testing="testing"; 
    Toast.makeText(getApplicationContext(), result_var+testing, Toast.LENGTH_LONG).show(); 

根據你要不要用爲:

// if you have values of field1_int and field2_int 

int result_var=field1_int+field2_int; 

    Toast.makeText(getApplicationContext(),String.valueOf(result_var), Toast.LENGTH_LONG).show(); 
+0

謝謝,我知道了:) – user2776638

0

試試這個吐司消息必須是String

Toast.makeText(getApplicationContext(), ""+result_var, Toast.LENGTH_LONG).show(); 
+0

謝謝:)我明白了 – user2776638

1

問題是你需要一個字符串顯示在祝酒。您正試圖顯示一個int。最簡單的做法是用String.valueOf()用String來包裝它。

Toast.makeText(getApplicationContext(), String.valueOf(result_var), Toast.LENGTH_LONG).show(); 

作爲java的一個備註,我們不會在變量名中使用下劃線_。最好把它們命名爲這樣。

EditText mInputField1,mInputField2,mResultField; 
int mResultVar; 

m爲成員變量。

最好還是把findViewById放在onCreate上。

0

建議,因爲你是初學者。您應該閱讀您正在使用的任何方法所接受的必需參數。

Read Toast

例如,在你的情況。

Toast.makeText(getApplicationContext(), ""+result_var, Toast.LENGTH_LONG).show(); 

或者

Toast.makeText(getApplicationContext(),String.valueOf(result_var), Toast.LENGTH_LONG).show();