• Android开发之简单计算器


    运行截图

    点击"相加"按钮

    ViewActivity.java

    package com.archie.activity03;

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

    public class ViewActivity extends Activity {
    private TextView textView;
    private EditText editText1;
    private EditText editText2;

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //编辑框
    editText1=(EditText) findViewById(R.id.numberOne);
    editText2
    =(EditText) findViewById(R.id.numberTwo);

    //文本
    textView=(TextView) findViewById(R.id.textView);
    textView.setText(
    "");
    //按钮
    btn1=(Button) findViewById(R.id.btn1);
    btn1.setText(
    "相加");
    //绑定监听器对象
    btn1.setOnClickListener(new Btn1CalListener());

    btn2
    =(Button) findViewById(R.id.btn2);
    btn2.setText(
    "相减");
    btn2.setOnClickListener(
    new Btn2CalListener());

    btn3
    =(Button) findViewById(R.id.btn3);
    btn3.setText(
    "相乘");
    btn3.setOnClickListener(
    new Btn3CalListener());

    btn4
    =(Button) findViewById(R.id.btn4);
    btn4.setText(
    "相除");
    btn4.setOnClickListener(
    new Btn3CalListener());
    }
    //加法
    class Btn1CalListener implements OnClickListener{

    @Override
    public void onClick(View v) {
    //获取两个EditText的值
    String numberOne=editText1.getText().toString();
    String numberTwo
    =editText2.getText().toString();
    //将获取的值存放到Intent对象当中,以便传递
    Intent intent=new Intent();
    intent.putExtra(
    "one", numberOne);
    intent.putExtra(
    "two", numberTwo);

    //运算符
    intent.putExtra("calculate", "+");

    intent.setClass(ViewActivity.
    this, ResultActivity.class);
    //启动ResultActivity
    ViewActivity.this.startActivity(intent);
    }

    }
    //减法
    class Btn2CalListener implements OnClickListener{

    @Override
    public void onClick(View v) {
    //获取两个EditText的值
    String numberOne=editText1.getText().toString();
    String numberTwo
    =editText2.getText().toString();
    //将获取的值存放到Intent对象当中,以便传递
    Intent intent=new Intent();
    intent.putExtra(
    "one", numberOne);
    intent.putExtra(
    "two", numberTwo);

    //运算符
    intent.putExtra("calculate", "-");

    intent.setClass(ViewActivity.
    this, ResultActivity.class);
    //启动ResultActivity
    ViewActivity.this.startActivity(intent);
    }

    }
    //乘法
    class Btn3CalListener implements OnClickListener{
    @Override
    public void onClick(View v) {
    //获取两个EditText的值
    String numberOne=editText1.getText().toString();
    String numberTwo
    =editText2.getText().toString();
    //将获取的值存放到Intent对象当中,以便传递
    Intent intent=new Intent();
    intent.putExtra(
    "one", numberOne);
    intent.putExtra(
    "two", numberTwo);

    //运算符
    intent.putExtra("calculate", "*");

    intent.setClass(ViewActivity.
    this, ResultActivity.class);
    //启动ResultActivity
    ViewActivity.this.startActivity(intent);
    }
    }
    //除法
    class Btn4CalListener implements OnClickListener{
    @Override
    public void onClick(View v) {
    //获取两个EditText的值
    String numberOne=editText1.getText().toString();
    String numberTwo
    =editText2.getText().toString();
    //将获取的值存放到Intent对象当中,以便传递
    Intent intent=new Intent();
    intent.putExtra(
    "one", numberOne);
    intent.putExtra(
    "two", numberTwo);

    //运算符
    intent.putExtra("calculate", "/");

    intent.setClass(ViewActivity.
    this, ResultActivity.class);
    //启动ResultActivity
    ViewActivity.this.startActivity(intent);
    }
    }
    }

    ResultActivity.java

    package com.archie.activity03;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;

    public class ResultActivity extends Activity{

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
    textView
    =(TextView) findViewById(R.id.result);

    //得到Intent对象
    Intent intent=getIntent();
    int one=Integer.parseInt(intent.getStringExtra("one"));
    int two=Integer.parseInt(intent.getStringExtra("two"));

    String calculate
    =intent.getStringExtra("calculate");
    int result=0;
    //加法
    if(calculate.equals("+")){
    result
    =one+two;
    textView.setText(result
    +"");
    }
    //减法
    else if(calculate.equals("-")){
    result
    =one-two;
    textView.setText(result
    +"");
    }
    //乘法
    else if(calculate.equals("*")){
    result
    =one*two;
    textView.setText(result
    +"");
    }
    //除法
    else if(calculate.equals("/")){
    try {
    result
    =one/two;
    textView.setText(result
    +"");
    }
    catch (Exception e) {
    textView.setText(
    "除数不能为零");
    e.printStackTrace();
    }
    }

    }


    }

  • 相关阅读:
    MutationObserver DOM变化的观察
    lspci详解分析
    dpdk快速编译使用
    bonding的系统初始化介绍
    fio测试nvme性能
    [驱动] 一个简单内核驱动,通过qemu调试(1)
    qemu启动vm后,如何host上使用ssh连接?
    Linux C下变量和常量的存储的本质
    从计算机中数据类型的存储方式,思考理解原码,反码,补码
    Linux C动态链接库实现一个插件例子
  • 原文地址:https://www.cnblogs.com/archie2010/p/2044901.html
Copyright © 2020-2023  润新知