Java 创建计算器Android应用程序

Java 创建计算器Android应用程序,java,android,xml,eclipse,android-layout,Java,Android,Xml,Eclipse,Android Layout,我正在学习android编程,我只是不知道如何将我的xml转换成一个可以工作的计算器 这是我的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="

我正在学习android编程,我只是不知道如何将我的xml转换成一个可以工作的计算器

这是我的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"
tools:context="com.afm.calculator.MainActivity" >

<TextView
    android:id="@+id/tvresult"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="40dp" />

<Button
    android:id="@+id/times"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="x"
    android:textSize="40sp" />

<Button
    android:id="@+id/plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:textSize="40dp" />

<Button
    android:id="@+id/divide"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="÷"
    android:textSize="40sp" />

<Button
    android:id="@+id/mines"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-"
    android:textSize="40sp" />

<Button
    android:id="@+id/equal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="="
    android:textSize="40sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" >

</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" />

</RelativeLayout>

请帮助我完成它,并提前thx

使用android:onClick属性在xml中为每个按钮分配一个方法

例如android:onClick=doSomething。单击此按钮时,将调用doSomething方法


您的方法应该从TextFields中获取值,并在这些方法中执行操作。然后在TextView中显示结果。首先,使用setOnClickListener方法将Listener添加到所有按钮

例如:plus.setOnClickListenerthis

对onClick方法进行如下修改

public void onClick(View view) {        

    if (view == mines) {
        // do the calculation here
        //update the TextView using tv.setText()
    } else if (view == plus) {
        // do the calculation here
        //update the TextView 
    }
    //.. So on for all buttons

}

thx这真的很有帮助
public void onClick(View view) {        

    if (view == mines) {
        // do the calculation here
        //update the TextView using tv.setText()
    } else if (view == plus) {
        // do the calculation here
        //update the TextView 
    }
    //.. So on for all buttons

}