2014-11-06 85 views
0

我試圖在Eclipse上創建一個計算器作爲Android應用程序。 我在監聽多個按鈕時遇到了一些麻煩。 我知道互聯網上有很多解決方案可用於該項目,但我正在學習,所以我非常想知道我的錯誤在哪裏。一個聽衆 - 多個按鈕

這裏是我的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="com.rayana.calculator.MainActivity" > 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="39dp" 
     android:text="Result =" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_marginBottom="31dp" 
     android:text="B =" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignLeft="@+id/textView2" 
     android:text="A =" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/editText2" 
     android:layout_alignLeft="@+id/editText2" 
     android:layout_marginBottom="45dp" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView2" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignLeft="@+id/button2" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView3" 
     android:layout_toRightOf="@+id/textView3" 
     android:text="-" /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/textView3" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="16dp" 
     android:text="+" /> 

    <Button 
     android:id="@+id/button3" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_marginLeft="16dp" 
     android:layout_toRightOf="@+id/button2" 
     android:text="x" /> 

    <Button 
     android:id="@+id/button4" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_marginLeft="16dp" 
     android:layout_toRightOf="@+id/button3" 
     android:text="÷" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textView3" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

</RelativeLayout> 

這裏是我的Main.java:

import android.support.v7.app.ActionBarActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.os.Bundle; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button plus = (Button)findViewById(R.id.button1); 
     Button minus = (Button)findViewById(R.id.button2); 
     Button multiply = (Button)findViewById(R.id.button3); 
     Button devise = (Button)findViewById(R.id.button4); 

     final EditText editText1 = (EditText)findViewById(R.id.editText1); 
     final double A = Double.valueOf(editText1.getText().toString()); 
     final EditText editText2 = (EditText)findViewById(R.id.editText2); 
     final double B = Double.valueOf(editText2.getText().toString()); 
     final EditText editText3 = (EditText)findViewById(R.id.editText3); 


     plus.setOnClickListener(Operation1); 
     minus.setOnClickListener(Operation1); 
     multiply.setOnClickListener(Operation1); 
     devise.setOnClickListener(Operation1); 


     OnClickListener Operation1 = new OnClickListener() { 

     public void onClick(View v) { 

       switch(v.getId()){ 

       case R.id.button1: 

        double result = A+B; 
        editText3.setText(String.valueOf(result)); 
        break; 
       case R.id.button2: 
        double result2 = A-B; 
        editText3.setText(String.valueOf(result2)); 
        break; 
       case R.id.button3: 
        double result3 = A*B; 
        editText3.setText(String.valueOf(result3)); 
        break; 
       case R.id.button4: 
        double result4 = A/B; 
        editText3.setText(String.valueOf(result4)); 
        break; 
       } 
      } 
    }; 
    } 
+0

你有什麼麻煩? – 2014-11-06 12:54:20

+0

「我遇到了一些麻煩」是什麼,你有什麼錯誤? – 2014-11-06 12:54:22

+0

請提一下這個問題。 – Kirtan 2014-11-06 13:04:54

回答

1

寫您的打擊代碼的onCreate()方法之外。

public class MainActivity extends ActionBarActivity { 

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



    Button plus = (Button)findViewById(R.id.button1); 
    Button minus = (Button)findViewById(R.id.button2); 
    Button multiply = (Button)findViewById(R.id.button3); 
    Button devise = (Button)findViewById(R.id.button4); 

    final EditText editText1 = (EditText)findViewById(R.id.editText1); 
    final double A = Double.valueOf(editText1.getText().toString()); 
    final EditText editText2 = (EditText)findViewById(R.id.editText2); 
    final double B = Double.valueOf(editText2.getText().toString()); 
    final EditText editText3 = (EditText)findViewById(R.id.editText3); 


    plus.setOnClickListener(Operation1); 
    minus.setOnClickListener(Operation1); 
    multiply.setOnClickListener(Operation1); 
    devise.setOnClickListener(Operation1); 

} 
     ///Your click listener method: 
OnClickListener Operation1 = new OnClickListener() { 

     public void onClick(View v) { 

       switch(v.getId()){ 

       case R.id.button1: 

        double result = A+B; 
        editText3.setText(String.valueOf(result)); 
        break; 
       case R.id.button2: 
        double result2 = A-B; 
        editText3.setText(String.valueOf(result2)); 
        break; 
       case R.id.button3: 
        double result3 = A*B; 
        editText3.setText(String.valueOf(result3)); 
        break; 
       case R.id.button4: 
        double result4 = A/B; 
        editText3.setText(String.valueOf(result4)); 
        break; 
       } 

      } 
    }; 
} 
+0

我嘗試過,但是然後editText3,A和B不再被識別。消息是:'editText3無法解析'和A和B相同。 – user3855955 2014-11-06 13:45:04

+0

請發佈您的完整logcat錯誤。 @ user3855955 – GrIsHu 2014-11-07 04:38:25