我是一個初學者的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>
你有一個logcat的錯誤?如果是這樣,請包括它。 – Tigger
做了'result_field.setText(「result」+ result_var);'工作? – Eoin
'editText1'未定義 – Eoin