简易的android文本式计算器

XML文件代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
   //为布局文件中添加如下三个TextView,具体如下
   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="35dp"
        android:text="第一个数:" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="40dp"
        android:text="第二个数:" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="44dp"
        android:text=" 结   果:" />
     //为布局文件添加三个EditText,分别用来输入第一第二个数和显示计算结果
    <EditText
        android:id="@+id/Three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/One"
        android:layout_alignParentRight="true"
        android:ems="10" />
    <EditText
        android:id="@+id/Two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/One"
        android:layout_alignRight="@+id/Three"
        android:ems="10" />
  <EditText
        android:id="@+id/One"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />
    //添加四个Button按钮,分别为+,-,*,/ 控制按钮
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="34dp"
        android:text="+" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/button1"
        android:layout_marginTop="30dp"
        android:text="-" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="22dp"
        android:layout_toRightOf="@+id/button2"
        android:text="*" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignLeft="@+id/button3"
        android:text="/" />
</RelativeLayout>



在java文件中具体代码如下:


package com.manchli003;

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


public class MainActivity extends Activity {

//定义4个Button 控件,用来加减乘除运算
     private Button bn1;
     private Button bn2;
     private Button bn3;
     private Button bn4;

    //定义3个Editext 控件,用来接收输入数字并显示输出计算的结果
     private EditText edit1;
     private EditText edit2;
     private EditText edit3;
   //重写onCreate方法,此方法将为Activity创建所引用的函数
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //通过id分别获得EditText控件
        edit1=(EditText)findViewById(R.id.One);
        edit2=(EditText)findViewById(R.id.Two);
        edit3=(EditText)findViewById(R.id.Three);
      
        //通过id分别获得Button控件
                         bn1=(Button)findViewById(R.id.button1);
        bn2=(Button)findViewById(R.id.button2);
        bn3=(Button)findViewById(R.id.button3);
        bn4=(Button)findViewById(R.id.button4); 
  
        //绑定每个Button控件监听器  
        bn1.setOnClickListener(new addListener());
        bn2.setOnClickListener(new reduceListener());
        bn3.setOnClickListener(new multiplyListener());
        bn4.setOnClickListener(new divisionListener());
}
//创建每个Butoon的监听器
     class addListener implements OnClickListener{
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
        //获取Editext 控件中输入的值
                double number1=Double.parseDouble(edit1.getText().toString());
                double number2=Double.parseDouble(edit2.getText().toString());
                double number=number1+number2;
                edit3.setText(""+number);
         }
           }
           
     class reduceListener implements OnClickListener{
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
                double number1=Double.parseDouble(edit1.getText().toString());
                double number2=Double.parseDouble(edit2.getText().toString());
                double number=number1-number2;
                edit3.setText(""+number);
         }
           }  
      class multiplyListener implements OnClickListener{
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
              double number1=Double.parseDouble(edit1.getText().toString());
              double number2=Double.parseDouble(edit2.getText().toString());
              double number=number1*number2;
              edit3.setText(""+number);
         }
           }
      class divisionListener implements OnClickListener{
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
               double number1=Double.parseDouble(edit1.getText().toString());
               double number2=Double.parseDouble(edit2.getText().toString());
               double number=number1/number2;
               edit3.setText(""+number);
         }
            }             
}
注意:此计算器要输入两个数后在按所要的计算运算符    
在虚拟中运行得到如下界面:



   在测试中发现无法计算无法计算带小数点的数值,当输入带小数点的数时,运行出错,程序关闭,其他输入的整数计算都没有发现问题。此出错问题有待更一步的探讨。

     上面问题已经解决,解决该问题把原来代码Integer.parseInt改为代码中红色的代码

在学习的过程中我还尝试用线性布局来做,但由于个人水平,做出来布局界面非常乱,面目全非,图如下:

 总结:  要考虑布局,善用布局管理器,这样效率会更高,效果也会更好。  
       

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
蛋白质是生物体中普遍存在的一类重要生物大分子,由天然氨基酸通过肽键连接而成。它具有复杂的分子结构和特定的生物功能,是表达生物遗传性状的一类主要物质。 蛋白质的结构可分为四级:一级结构是组成蛋白质多肽链的线性氨基酸序列;二级结构是依靠不同氨基酸之间的C=O和N-H基团间的氢键形成的稳定结构,主要为α螺旋和β折叠;三级结构是通过多个二级结构元素在三维空间的排列所形成的一个蛋白质分子的三维结构;四级结构用于描述由不同多肽链(亚基)间相互作用形成具有功能的蛋白质复合物分子。 蛋白质在生物体内具有多种功能,包括提供能量、维持电解质平衡、信息交流、构成人的身体以及免疫等。例如,蛋白质分解可以为人体提供能量,每克蛋白质能产生4千卡的热能;血液里的蛋白质能帮助维持体内的酸碱平衡和血液的渗透压;蛋白质是组成人体器官组织的重要物质,可以修复受损的器官功能,以及维持细胞的生长和更新;蛋白质也是构成多种生理活性的物质,如免疫球蛋白,具有维持机体正常免疫功能的作用。 蛋白质的合成是指生物按照从脱氧核糖核酸(DNA)转录得到的信使核糖核酸(mRNA)上的遗传信息合成蛋白质的过程。这个过程包括氨基酸的活化、多肽链合成的起始、肽链的延长、肽链的终止和释放以及蛋白质合成后的加工修饰等步骤。 蛋白质降解是指食物中的蛋白质经过蛋白质降解酶的作用降解为多肽和氨基酸然后被人体吸收的过程。这个过程在细胞的生理活动中发挥着极其重要的作用,例如将蛋白质降解后成为小分子的氨基酸,并被循环利用;处理错误折叠的蛋白质以及多余组分,使之降解,以防机体产生错误应答。 总的来说,蛋白质是生物体内不可或缺的一类重要物质,对于维持生物体的正常生理功能具有至关重要的作用。
好的,我可以为您提供一个解决方案。你可以参考下面的代码: 在activity_main.xml中,可以这样设计UI: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入第一个数字" android:inputType="number"/> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入第二个数字" android:inputType="number"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="="/> <EditText android:id="@+id/editText3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="结果" android:enabled="false"/> </LinearLayout> ``` 在MainActivity.java中,可以这样实现计算器: ``` public class MainActivity extends AppCompatActivity { private EditText editText1, editText2, editText3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1 = findViewById(R.id.editText1); editText2 = findViewById(R.id.editText2); editText3 = findViewById(R.id.editText3); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1 = Integer.parseInt(editText1.getText().toString()); int num2 = Integer.parseInt(editText2.getText().toString()); int result = num1 + num2; editText3.setText(String.valueOf(result)); } }); } } ``` 这样,当用户在前两个EditText中输入整数,并单击“=”按钮时,程序会在第三个EditText中显示这两个数之和。希望这个解决方案能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值