利用android studio制作简易的计算器

      • Android Studio简介
      • 正文
        • 设计思路
        • 上代码
        • colors.xml代码

Android Studio简介

Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试。
在IDEA的基础上,Android Studio 增加了:

  • 基于Gradle的构建支持
  • Android 专属的重构和快速修复
  • 提示工具以捕获性能、可用性、版本兼容性等问题
  • 支持ProGuard 和应用签名
  • 基于模板的向导来生成常用的 Android 应用设计和组件
  • 功能强大的布局编辑器,可以让你拖拉 UI 控件并进行效果预览

正文

我这里提供一个计算器ui参考图:

设计思路

在设计的时候,我们可以根据要设计的程序,采用适合的布局,在Android中有七大布局,分别是:

  • LinearLayout(线性布局)
  • RelativeLayout(相对布局)
  • TableLayout(表格布局)
  • FrameLayout(帧布局)
  • AbsoluteLayout(绝对布局)
  • GridLayout(网格布局)
  • ConstraintLayout(约束布局)

目前android最新的默认布局是ConstraintLayout(约束布局)

上代码

这么多布局中,最适合做计算器的,是网格布局,因为计算器中有很多按钮,如果用其他的布局会增加很多代码,其效果也是一样的。

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_row="4"    android:stretchColumns="3">        <EditText            android:layout_width="match_parent"            android:layout_height="200dp"            android:editable="false"            android:inputType="number"            android:textSize="30dp"            android:background="@color/text_bk2">        </EditText>                <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text1"                android:layout_weight="1"                android:text="mc" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text1"                android:layout_weight="1"                android:text="m+" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text1"                android:layout_weight="1"                android:text="m-" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text1"                android:layout_weight="1"                android:text="mr" />        </TableRow>        <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="C" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="/" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="X" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="del" />        </TableRow>                <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="7" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="8" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="9" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="-" />        </TableRow>        <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="4" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="5" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="6" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="+" />        </TableRow>        <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="1" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="2" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="3" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk1"                android:textColor="@color/text_text3"                android:layout_weight="1"                android:text="%" />        </TableRow>        <TableRow>            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="00" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="0" />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk2"                android:textColor="@color/text_text2"                android:layout_weight="1"                android:text="." />            <Button                android:layout_width="0dp"                android:layout_height="65dp"                android:textSize="20dp"                android:background="@color/text_bk3"                android:textColor="@color/text_text4"                android:layout_weight="1"                android:text="=" />        </TableRow>    </TableLayout>

colors.xml代码

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="colorPrimary">#6200EE    <color name="colorPrimaryDark">#3700B3    <color name="colorAccent">#03DAC5    <color name="text_bk1">#F5F5F5    <color name="text_bk2">#FCFCFC    <color name="text_bk3">#5ADCED    <color name="text_text1">#878787    <color name="text_text2">#000000    <color name="text_text3">#5ADCED    <color name="text_text4">#FCFCFC</resources>

运行结果:


我是ots-luo,码字不易,写教程也不易,如果觉得文章不错,可以点赞评论,感谢支持!!


更多文章记得关注我的博客
网站文章对应:点击传送

——————————END——————————

更多相关文章

  1. Android(安卓)五大布局
  2. android 音频系统java部分代码阅读
  3. 实现在一个界面里多个TextView的跑马灯效果
  4. Android(安卓)用style简化layout布局文件
  5. Android高手进阶教程(五)之----Android(安卓)中LayoutInflater的
  6. android 自定义 permission 权限
  7. 使用TextView/EditText应该注意的地方
  8. android中的runOnUiThread(runnable)
  9. Android(安卓)Service AIDL 远程调用服务之简单音乐播放实例

随机推荐

  1. Android签名证书生成
  2. Android有用代码片段2
  3. 【android】用camera实现view的伪3D的效
  4. H5调用Android原生Api
  5. Android View绘制及事件(一)屏幕的视图层级
  6. android sdk 中apidemo如何加载到eclipse
  7. Android(安卓)studio导入eclipse项目
  8. TextView属性大全!技术干货
  9. 关于移动端踩过的坑
  10. Android NDK 使用第一步,编译c文件,声明jni