Android开发学习笔记--一个有界面A+B的计算器
做了一个A+B的APP,虽然很简单,但是作为初学者还是弄了几个小时才弄好,什么东西都要看书或者百度,但最后成功了,还是很开心的,收货蛮大的。现在把过程写一下:
首先给出效果图:
一开始布局一直有问题,不知道为什么我定义了两个编辑框跟一个按钮,但画出来的时候全都重叠在左上角了,只能输入到一个编辑框,一直卡在这里,后来找了一个输入用户名密码的布局文件参考了一下,发现把原来生成的前面那些删掉,然后设置为垂直布局就不会重叠在一起了,正常画出来之后,代码部分就简单了,一共有三个变量,我把第三个显示结果的框设置成了只读的属性,设置的方法是:android:editable="false"
activity_main.xml如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="请输入第一个数:" 11 /> 12 <EditText 13 android:id="@+id/num1" 14 android:textColorHint="#ff2323" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:hint="num" 18 /> 19 <TextView 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="请输入第二个数:" 23 /> 24 <EditText 25 android:id="@+id/num2" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:hint="num" 29 /> 30 <TextView 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:text="结果:" 34 /> 35 <EditText 36 android:id="@+id/num3" 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:editable="false" 40 android:hint="结果" 41 /> 42 43 <Button android:id="@+id/button1" 44 android:onClick="button_click" 45 android:layout_width="fill_parent" 46 android:layout_height="wrap_content" 47 android:text="@string/hello_world" 48 /> 49 50 </LinearLayout>
View Code
MainActivity.java如下:
1 package com.example.hehe; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 import android.view.View; 8 9 import android.widget.Button; 10 import android.widget.EditText; 11 12 import android.widget.Toast; 13 import android.app.Activity; 14 import android.graphics.Color; 15 import android.os.Bundle; 16 import android.provider.CalendarContract.Colors; 17 import android.view.KeyEvent; 18 import android.view.View; 19 import android.view.View.OnKeyListener; 20 import android.widget.EditText; 21 import android.widget.TextView; 22 23 public class MainActivity extends Activity { 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 } 29 30 31 @Override 32 public boolean onCreateOptionsMenu(Menu menu) { 33 // Inflate the menu; this adds items to the action bar if it is present. 34 getMenuInflater().inflate(R.menu.main, menu); 35 return true; 36 } 37 public void button_click(View v) 38 { 39 // Button button1 = (Button) findViewById(R.id.button1); 40 Button button1 = (Button)v; 41 42 EditText num1 = (EditText) findViewById(R.id.num1); 43 EditText num2 = (EditText) findViewById(R.id.num2); 44 EditText num3 = (EditText) findViewById(R.id.num3); 45 int a = Integer.parseInt(num1.getText().toString()); 46 int b = Integer.parseInt(num2.getText().toString()); 47 int c = a + b; 48 String te = ""+a+"+"+b+"="+c; 49 num3.setText(te); 50 num3.setTextColor(Color.GREEN); 51 } 52 }
View Code
转载于:https://www.cnblogs.com/xiaxiaosheng/p/3978136.html
Android开发学习笔记--一个有界面A+B的计算器相关推荐
- Android 开发学习笔记
Android 开发学习笔记 Lesson1 (2.28) android之父: Andy Rubin(安迪 鲁宾) Lesson2 (3.3) android 的优点: 开放.网络接入自由.丰富的硬 ...
- Android 开发学习笔记:七大知识点板块汇总
前言 我从事 Android 开发行业也有些年头,工作期间也接触过很多 Android 开发者, 因此也非常清楚 程序员最大的限制并非年龄而是实力: 但大多数初中级Android工程师,想要提升技能, ...
- Android开发学习笔记---搭建Android开发环境
今天突发奇想,想看看Android开发是什么样的,于是就在度娘了一下Android开发,便自己动手搭建了起来.中间也碰到了一些问题,自己慢慢度娘,谷哥去解决.现在把搭建步骤记录如下: 第一步:下载Ec ...
- Android开发学习笔记(二)——编译和运行原理(1)
接着上一篇的内容,继续从全局了解Android.在清楚了Android的平台架构(可以看作是静态原理)后,还需要掌握其动态原理.动态原理包含两部分,一部分是编译原理,另一部分是运行原理.有人会说,搭建 ...
- 【Android开发学习笔记之一】5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- Android开发学习笔记
2022.3.10 android开发笔记 Lesson4(3.10web). 作业: 手写MainAcitivity.java与activity_ main.xml 文件 res目录. 存放资源:图 ...
- android开发学习笔记系列(6)--代码规范
在开发android的时候,我对自己写的代码很是不满,原因在于自己看到别人的代码,很是头痛,原因很简单,别人写的代码,我就要去猜他的意思,极其烦恼,嗯,就是他没有遵循代码规范,因此我在博客园上寻找一篇 ...
- Android开发学习笔记:Gallery和GridView浅析
一.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery还可以和ImageSwitcher组件结合使用来 ...
- Android开发学习笔记(二)——编译和运行原理(2)
上篇介绍了应用程序从源码到安装包的整个过程,考虑到篇幅过长,遂将原理内容分成两部分来介绍.接下来,在本篇中,主要介绍Android平台应用程序的运行原理. 在阅读了SDK文档中"Applic ...
最新文章
- php sql count函数,SQL COUNT() 函数
- 独家 | 教你使用Keras on Google Colab(免费GPU)微调深度神经网络
- [*开同*看] 星际情书
- VS2008 解决方案配置器,即Debug,Release 模式选择
- Python TCP 学习笔记
- 多序列比对要多久时间_多序列比对软件Clustalw使用方法
- Android编译gradle提示A problem occurred evaluating project ':example'.
- ddrk(ddrk)
- RV1126 调试记录
- python米和厘米转换代码_用Python写一个朴素的长度单位转换器
- 华为p10和p10plus区别_华为p10和p10plus的区别 华为p10plus和华为p10对比评测
- MyHDL中文手册(十)——转换成Verilog和VHDL
- 机器人的未来发展的趋势
- C练题笔记之:牛客-HJ033.整数与IP地址间的转换
- 有哪些改图方便的软件?6款P图软件来看看
- 利用NT6.X HDD Installer_v3.0.exe安装电脑系统
- k30最小宽度380不管用了_黄金分割:用数学让你的摄影构图,更有意思!
- ubuntu键盘输入中文延迟卡顿问题
- 个人笔记018--JQ实现多文件上传
- 关于商铺转让费的思考