zoukankan      html  css  js  c++  java
  • MutiButton Application calculator.

    Brief introduction:

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

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

    Download project link : Download   

    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 .

    pic1

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

    pic3

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

    pic2

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

    pic4

    pic5

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

    For more questions , contacts me by :

    cquptzx@qq.com or  cquptzx@outlook.com

  • 相关阅读:
    研究生毕业论文如何选题
    关于虚拟机中开vmnet1(host)后pc不能上网的问题解决
    ubuntu10.04安装x264库
    CentOS 5.5无法识别Atheros AR8151网卡问题解决
    dm365工作笔记20130731
    DAVINCI DM365-DM368开发攻略——U-BOOT-2010.12及UBL的移植
    SQL语法练习
    T-SQL查询语句常用优化技巧总结
    【转】Asp.net控件开发学习笔记整理篇
    【转】Asp.net控件开发学习笔记整理篇
  • 原文地址:https://www.cnblogs.com/xilifeng/p/2633551.html
Copyright © 2011-2022 走看看