如何在Android中使用简单的计算器?如何使用“按钮"和单个编辑文本? [英] How do a simple calculator in Android? How to do it using Buttons and single edit text?

查看:96
本文介绍了如何在Android中使用简单的计算器?如何使用“按钮"和单个编辑文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Android,因此我想使事情保持简单.

I am learning Android so I want to keep things simple.

我必须添加两个数字并将结果显示在单个编辑框中.要与第二个数字相加,您必须将第一个数字存储在变量中.然后,在编辑文本中使用此变量和第二个数字进行添加.

I have to add two numbers and display the result in a single editbox. To add with the second number you have store the first number in a variable. Then later add using this variable with the second number in the edit text.

如果您查看下面的xml和Java代码,将会有一个更好的主意.提前致谢.

If you look at the xml and Java Code below you will have a better idea. Thanks in advance.

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_gravity="bottom">
    <Button android:text="1" android:padding="20px" android:layout_marginTop="38dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_alignParentLeft="true"></Button>
    <Button android:text="2" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button1" android:id="@+id/button2" android:layout_toRightOf="@+id/button1"></Button>
    <Button android:text="3" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button2" android:id="@+id/button3" android:layout_toRightOf="@+id/button2"></Button>
    <Button android:text="4" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button3" android:id="@+id/button4" android:layout_toRightOf="@+id/button3"></Button>
    <Button android:text="5" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button5" android:layout_below="@+id/button1" android:layout_alignParentLeft="true"></Button>
    <Button android:text="6" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button5" android:layout_alignLeft="@+id/button2" android:id="@+id/button6"></Button>
    <Button android:text="7" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button6" android:layout_alignLeft="@+id/button3" android:id="@+id/button7"></Button>
    <Button android:text="8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button4" android:id="@+id/button8" android:layout_below="@+id/button4"></Button>
    <Button android:text="=" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button11" android:id="@+id/button15" android:layout_below="@+id/button12"></Button>
<Button android:text="clear" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignBottom="@+id/button15" android:layout_alignLeft="@+id/button12" android:id="@+id/button16" android:layout_below="@+id/button12"></Button>
    <Button android:text="-" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button12" android:layout_below="@+id/button8"></Button>
    <Button android:text="+" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button15" android:layout_toLeftOf="@+id/button16" android:id="@+id/button11"></Button>
    <Button android:text="/" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button7" android:layout_alignTop="@+id/button15" android:id="@+id/button14"></Button>
    <Button android:text="0" android:layout_alignRight="@+id/button14" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button14" android:id="@+id/button0"></Button>
    <Button android:text="*" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button10" android:layout_alignTop="@+id/button14" android:id="@+id/button13"></Button>
    <Button android:text="9" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button13" android:id="@+id/button9" android:layout_alignParentLeft="true"></Button>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/button14"></EditText>
  </RelativeLayout>
</LinearLayout>

Java代码

package calci.tor;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CalculatorActivity extends Activity {
    /** Called when the activity is first created. */
    public EditText display;  
    TextView edt;
    Integer c;
    String a="",b;
    //int d=0;

    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    edt=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b4=(Button)findViewById(R.id.button4);
    b5=(Button)findViewById(R.id.button5);
    b6=(Button)findViewById(R.id.button6);
    b7=(Button)findViewById(R.id.button7);
    b8=(Button)findViewById(R.id.button8);
    b9=(Button)findViewById(R.id.button9);
    b10=(Button)findViewById(R.id.button0);
    b11=(Button)findViewById(R.id.button11);
    b12=(Button)findViewById(R.id.button12);
    b13=(Button)findViewById(R.id.button13);
    b14=(Button)findViewById(R.id.button14);
    b15=(Button)findViewById(R.id.button15);
    b16=(Button)findViewById(R.id.button16);



    b1.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            a=edt.getText().toString();
            edt.setText(a+ "1");



        }
    });

    b2.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    a=edt.getText().toString();
                    edt.setText(a+ "2");
                                        }
            });

    b3.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    a=edt.getText().toString();
                    edt.setText(a+ "3");
                                        }
            });


    b4.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    a=edt.getText().toString();
                    edt.setText(a+ "4");

                                        }
            });


    b5.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {

                    a=edt.getText().toString();
                    edt.setText(a+ "5");

                }
            });


    b6.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    a=edt.getText().toString();
                    edt.setText(a+ "6");
                                        }
            });


    b7.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {

                    a=edt.getText().toString();
                    edt.setText(a+ "7");
                                        }
            });


    b8.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    a=edt.getText().toString();
                    edt.setText(a+ "8");

                }
            });


    b9.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    a=edt.getText().toString();
                    edt.setText(a+ "9");
                                        }
            });


    b10.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    a=edt.getText().toString();
                    edt.setText(a+ "0");

                }
            });


    b11.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                       String aa;
                      aa=a;      //To Store the first number displayed in edit text  to aa
                edt.setText("+");
                    edt.setText("");
                    //d=1;
                    c=Integer.parseInt(aa)+Integer.parseInt(a);




                }
            });





    b12.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    edt.setText("-");

                }
            });


    b13.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    edt.setText("*");
                }
            });



    b14.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    edt.setText("/");
                }
            });


    b15.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    //int c=Integer.parseInt(a)+Integer.parseInt(b);
                    //edt.setText(c);
                    display.setText(c);
                }
            });


    b16.setOnClickListener(new View.OnClickListener()
    {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                edt.setText("");
                }
    });
    }
}

推荐答案

//Below having correct programming .
//I did some changes in your code.This code was working nicely try this.
//`import android.os.Bundle;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 public EditText display;  
TextView edt;
Integer c,d,r,b;
String a="0",aa;


Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b4=(Button)findViewById(R.id.button4);
    b5=(Button)findViewById(R.id.button5);
    b6=(Button)findViewById(R.id.button6);
    b7=(Button)findViewById(R.id.button7);
    b8=(Button)findViewById(R.id.button8);
    b9=(Button)findViewById(R.id.button9);
    b10=(Button)findViewById(R.id.button0);
    b11=(Button)findViewById(R.id.button11);
    b12=(Button)findViewById(R.id.button12);
    b13=(Button)findViewById(R.id.button13);
    b14=(Button)findViewById(R.id.button14);
    b15=(Button)findViewById(R.id.button15);
    b16=(Button)findViewById(R.id.button16);



    b1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // TODO Auto-generated method stub

             a=edt.getText().toString();
             a=a+"1";
             edt.setText(a);

         }
     });

     b2.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"2";
                     edt.setText(a);
                                         }
             });

     b3.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     a=edt.getText().toString();
                     a=a+"3";
                     edt.setText(a);
                                         }
             });


     b4.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     a=edt.getText().toString();
                     a=a+"4";
                     edt.setText(a);

                                         }
             });


     b5.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {

                     a=edt.getText().toString();
                     a=a+"5";
                     edt.setText(a);

                 }
             });


     b6.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"6";
                     edt.setText(a);
                                         }
             });


     b7.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {

                     a=edt.getText().toString();
                     a=a+"7";
                     edt.setText(a);
                                         }
             });


     b8.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"8";
                     edt.setText(a);

                 }
             });


     b9.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"9";
                     edt.setText(a);
                                         }
             });


     b10.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"0";
                     edt.setText(a);
                 }
             });


 b11.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {

                       aa=a; 
                       b=1;    
                       a="";
                       edt.setText("+");

                     edt.setText("");
          }
             });





     b12.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {

                     aa=a;
                     b=2;
                     a="";
                     edt.setText("-");
                     edt.setText("");

                 }
             });


     b13.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     aa=a;
                     b=3;
                     a="";
                     edt.setText("*");
                     edt.setText("");

                 }
             });



     b14.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     aa=a;
                     b=4;
                     a="";
                     edt.setText("/");
                     edt.setText("");

                 }
             });


     b15.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {

                     if(b==1){
                       c=Integer.parseInt(aa);

                       d=Integer.parseInt(a);             
                       r=c+d;
                     }
                     else
                     if(b==4){
                         c=Integer.parseInt(aa);
                       d=Integer.parseInt(a);
                       r=c/d; 
                     }else
                         if(b==2){
                             c=Integer.parseInt(aa);
                           d=Integer.parseInt(a);
                           r=c-d; 
                         }
                         else
                             if(b==3){
                                 c=Integer.parseInt(aa);
                               d=Integer.parseInt(a);
                               r=c*d; 
                             }
                     Toast.makeText(MainActivity.this, "Result is::"+r, 10000).show();
                     c=0;
                     b=0;
                     d=0;
                     a="";
                     aa="";
                     edt.setText("");
                 }
             });


     b16.setOnClickListener(new View.OnClickListener()
     {

                 public void onClick(View v) {
                     // TODO Auto-generated method stub

                 edt.setText("");
                 }
             });




 }`

这篇关于如何在Android中使用简单的计算器?如何使用“按钮"和单个编辑文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆