Android——例子:简单计算器

今天没事干,做了个单击事件的练习。

截图如下:(一个小小的计算器)

XMl文件中的代码:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <TextView
 7         android:id="@+id/startTextView"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="请输入NUMBER:" />
11 
12     <EditText
13         android:id="@+id/num1"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:ems="10" />
17     <EditText
18         android:id="@+id/num2"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:ems="10" />
22     <LinearLayout
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content" >
25 
26         <Button
27             android:id="@+id/btnAdd"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="+" />
31 
32         <Button
33             android:id="@+id/btnSub"
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:text="-" />
37 
38         <Button
39             android:id="@+id/btnMul"
40             android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
42             android:text="×" />
43 
44         <Button
45             android:id="@+id/btnDiv"
46             android:layout_width="wrap_content"
47             android:layout_height="wrap_content"
48             android:text="÷" />
49     </LinearLayout>
50 
51     <TextView
52         android:id="@+id/showResult"
53         android:layout_width="match_parent"
54         android:layout_height="wrap_content"
55             />
56 
57 </LinearLayout>

Activity代码:

package com.example.clickproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EditText num1 = null;
    private EditText num2 = null;
    private TextView showResult = null;
    private Button btnAdd = null;
    private Button btnSub = null;
    private Button btnMul = null;
    private Button btnDiv = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        num1 = (EditText) super.findViewById(R.id.num1);
        num2 = (EditText) super.findViewById(R.id.num2);
        showResult = (TextView) super.findViewById(R.id.showResult);
        btnAdd = (Button) super.findViewById(R.id.btnAdd);
        btnSub = (Button) super.findViewById(R.id.btnSub);
        btnMul = (Button) super.findViewById(R.id.btnMul);
        btnDiv = (Button) super.findViewById(R.id.btnDiv);
        
        btnAdd.setOnClickListener(new AddOnclickListener());
        btnSub.setOnClickListener(new SubOnclickListener());
        btnMul.setOnClickListener(new MulOnclickListener());
        btnDiv.setOnClickListener(new DivOnclickListener());
    }
    /**
     * 加法监听子类
    * ClassName AddOnclickListener
    * Description TODO(这里用一句话描述这个类的作用)
    * @author 石头杨    北科维拓公司
    * date 2013-7-26 下午01:58:32
    *
     */
    private class AddOnclickListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
            float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
            float c = a+b;
            MainActivity.this.showResult.setText("结果:【"+a+"】 【+】 【"+b+"】 【=】"+c);
        }
        
    }
    /**
     * 减法监听子类
    * ClassName SubOnclickListener
    * Description TODO(这里用一句话描述这个类的作用)
    * @author 石头杨    北科维拓公司
    * date 2013-7-26 下午01:58:56
    *
     */
    private class SubOnclickListener implements OnClickListener{
        
        @Override
        public void onClick(View v) {
            float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
            float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
            float c = a-b;
            MainActivity.this.showResult.setText("结果:【"+a+"】 【-】 【"+b+"】 【=】"+c);
        }
        
    }
    /**
     * 乘法监听子类
    * ClassName MulOnclickListener
    * Description TODO(这里用一句话描述这个类的作用)
    * @author 石头杨    北科维拓公司
    * date 2013-7-26 下午01:59:11
    *
     */
    private class MulOnclickListener implements OnClickListener{
        
        @Override
        public void onClick(View v) {
            float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
            float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
            float c = a*b;
            MainActivity.this.showResult.setText("结果:【"+a+"】 【*】 【"+b+"】 【=】"+c);
        }
        
    }
    /**
     * 除法监听子类
    * ClassName DivOnclickListener
    * Description TODO(这里用一句话描述这个类的作用)
    * @author 石头杨    北科维拓公司
    * date 2013-7-26 下午01:59:22
    *
     */
    private class DivOnclickListener implements OnClickListener{
        
        @Override
        public void onClick(View v) {
            float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
            float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
            float c = a/b;
            MainActivity.this.showResult.setText("结果:【"+a+"】 【/】 【"+b+"】 【=】"+c);
        }
        
    }

}
逃避不一定躲得过,面对不一定最难过
原文地址:https://www.cnblogs.com/yangzhenlong/p/3216995.html