博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Muti-Button Application --calculator.
阅读量:7081 次
发布时间:2019-06-28

本文共 5330 字,大约阅读时间需要 17 分钟。

Brief introduction:

  • use Button , TextView and EditView together.
  • a method to add a OnClickListener to all Button.
  • some problems and solution.

================================

Download project link :    

included files are:

Calc.apk

Calc.zip (Souce Code)
result1.png
result2.png
result3.png
result4.png
================================

purpose: to make a calculator using Button,EditText,TextView.

Method one:

 

 

 

 

 

 

 

 

 

package com.cquptzx.Calc;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

publicclass CopyOfCalcActivity extends Activity

{

    /** Called when the activity is first created. */

   

    /*   + - * /  button.*/

    public Button btn2;

    public Button btn3;

    public Button btn4;

    public Button btn5;

 

    /* the two number to be input.*/

    public EditText et1;

    public EditText et2;

   

    /*  used to show the  +  -  *  /   */

    public TextView tv2;

    /* used to show the result. */

    public TextView tv4;

   

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        /* Find the ID of the Object.*/

        btn2 = (Button) findViewById(R.id.button2);

        btn3 = (Button) findViewById(R.id.button3);

        btn4 = (Button) findViewById(R.id.button4);

        btn5 = (Button) findViewById(R.id.button5);

       

        tv2 = (TextView) findViewById(R.id.textView2);

        tv4 = (TextView) findViewById(R.id.textView4);

       

        et1 = (EditText) findViewById(R.id.editText1);

        et2 = (EditText) findViewById(R.id.editText2); 

 

 

        btn2.setOnClickListener(new Button.OnClickListener ()

        {

           @Override

           publicvoid onClick(View v) {

              // TODO Auto-generated method stub

              tv2.setText("+");

              String str =

                     Float.toString

                     (

                     Float.parseFloat(et1.getText().toString()) +

                     Float.parseFloat(et2.getText().toString())                                                    

                     );

              tv4.setText(str);

           }       

        });

       

        btn3.setOnClickListener(new Button.OnClickListener ()

        {

           @Override

           publicvoid onClick(View v) {

              // TODO Auto-generated method stub

              tv2.setText("-");

              String str =

                     Float.toString

                     (

                     Float.parseFloat(et1.getText().toString()) -

                     Float.parseFloat(et2.getText().toString())                                                    

                     );

              tv4.setText(str);

           }       

        });

       

        btn4.setOnClickListener(new Button.OnClickListener ()

        {

           @Override

           publicvoid onClick(View v) {

              // TODO Auto-generated method stub

              tv2.setText("*");

              String str =

                     Float.toString

                     (

                     Float.parseFloat(et1.getText().toString()) *

                     Float.parseFloat(et2.getText().toString())                                                    

                     );

              tv4.setText(str);

           }       

        });

       

      

         btn5.setOnClickListener(new Button.OnClickListener ()

       

        {

           @Override

           publicvoid onClick(View v) {

              // TODO Auto-generated method stub

              tv2.setText("/");

              String str =

                     Float.toString

                     (

                     Float.parseFloat(et1.getText().toString()) /

                     Float.parseFloat(et2.getText().toString())                                                    

                     );

              tv4.setText(str);

           }       

        });  

        

    }  

}

Prbolem : btn2,btn3,btn4,btn5 have the similar OnClick method, how to use one OnClickLsitener to reach our goal? 

Solution:

Let the CalcActivity implements OnClickListener and relize the the OnClick method.

package com.cquptzx.Calc;

 

import android.app.Activity;

import android.view.View.OnClickListener;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

publicclass CalcActivity extends Activity implements OnClickListener

{

    /** Called when the activity is first created. */

   

    /*   + - * /  button.*/

    public Button btn2;

    public Button btn3;

    public Button btn4;

    public Button btn5;

 

    /* the two number to be input.*/

    public EditText et1;

    public EditText et2;

   

    /*  used to show the  +  -  *  /   */

    public TextView tv2;

    /* used to show the result. */

    public TextView tv4;

   

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        /* Find the ID of the Object.*/

        btn2 = (Button) findViewById(R.id.button2);

        btn3 = (Button) findViewById(R.id.button3);

        btn4 = (Button) findViewById(R.id.button4);

        btn5 = (Button) findViewById(R.id.button5);

       

        tv2 = (TextView) findViewById(R.id.textView2);

        tv4 = (TextView) findViewById(R.id.textView4);

       

        et1 = (EditText) findViewById(R.id.editText1);

        et2 = (EditText) findViewById(R.id.editText2); 

       

       btn2.setOnClickListener(this);

       btn3.setOnClickListener(this);

       btn4.setOnClickListener(this);

       btn5.setOnClickListener(this);

    }

 

    @Override

    publicvoid onClick(View v) {

       int   ID  = v.getId();

       Float A = Float.parseFloat(et1.getText().toString());

       Float B = Float.parseFloat(et2.getText().toString());

       String result =" ";

       switch(ID)

       {

       case R.id.button2:

           result = Float.toString(A + B);

           tv2.setText("+");

           break;

       case R.id.button3:

           result = Float.toString(A - B);

           tv2.setText("-");

           break;

       case R.id.button4:

           result= Float.toString(A * B);

           tv2.setText("*");

           break;

       case R.id.button5:

           result= Float.toString(A /B);

           tv2.setText("/");

           break;

       default: break;

       }

       tv4.setText(result);

    }

   

}

=======================================

There is a problem when to implements the OnclickListener:

when we write like this :

# public class CalcActivity extends Activity implements OnClickListener #

eclipse said there are errors and reminder us to import a package .

when we choosed the first one , the packages  we have  impoted are:

then we add the method as the eclipse has  pointed out ,but the parameters  are not we want.

 

the right method is to import the second package when we implements the OnClickListener. 

==================================================

 

 

 

 

For more questions , contacts me by :

or 

转载地址:http://hroml.baihongyu.com/

你可能感兴趣的文章
为什么Go不支持函数和运算的重载
查看>>
「征文」我和极光有个约会
查看>>
js鼠标滚轮事件
查看>>
java 调用摄像头
查看>>
阿里云maven库地址 和maven跳过测试 和常见maven命令
查看>>
Android网络防火墙实现初探
查看>>
欲保长寿,先补亏损 —胡海牙
查看>>
数据容量进制转换
查看>>
Spring Cloud Zuul过滤器详解
查看>>
使用DOM4J创建一个新的XML文件
查看>>
VIM使用系列:搜索功能
查看>>
SOAP--------Golang对接WebService服务实战
查看>>
7大维度看国外企业为啥选择gRPC打造高性能微服务?
查看>>
初创公司电商系统建立思考
查看>>
微服务框架Spring Cloud介绍 Part2: Spring Cloud与微服务
查看>>
linux系统下设置时间同步
查看>>
dubbo源码学习笔记----整体结构
查看>>
zipfile
查看>>
基于Dockerfile编译镜像并上传到Docker Hub公共仓库教程
查看>>
很形象地展示了进程与线程的区别
查看>>