一个听众 - 多个按钮

时间:2014-11-06 12:51:54

标签: android calculator listeners

我正在尝试在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;
                }
            }
    };
    }

1 个答案:

答案 0 :(得分: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;
                }

            }
    };
}