android布局与计算器app编程
来源:互联网 发布:java枚举类的定义 编辑:程序博客网 时间:2024/05/08 13:08
android布局分为:
1.线性布局LinearLayout
2.相对布局RelativeLayout
3.绝对布局AbsoluteLayout
4.帧布局FrameLayout
5.表格布局TableLayout
线性布局LinearLayout比较简单,元素可以水平顺序排放,也可以垂直顺序排放,元素内部也可以设置线性布局进行嵌套布局设置。
我的计算器app就是使用了嵌套线性布局来完成的。
代码如下:
<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:inputType="text" android:id="@+id/num_input" android:layout_width="match_parent" android:layout_height="80dp" /><LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="50dp" > <Button android:text="@string/one" android:id="@+id/one" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:background="#00FF00" /> <Button android:text="@string/two" android:id="@+id/two" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:background="#00FF00" /> <Button android:text="@string/three" android:id="@+id/three" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:background="#00FF00" /> <Button android:text="@string/cheng" android:id="@+id/jia" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:background="#00FF00" /></LinearLayout>
</LinearLayout>
这里只给出了一个输入框与一排按钮的代码,还有三排代码是重复的,参考嵌套布局。
界面布局设置好了之后,往按钮加上监听,然后别写计算代码就可以了。
监听与计算代码如下:
OnClickListener click = new OnClickListener(){@Overridepublic void onClick(View v) {Button bt = (Button)v;inContent = inContent + bt.getText();EditText et = (EditText) findViewById(R.id.num_input);et.setText(inContent);} }; public void in_guiling(View v){ inContent = ""; EditText et = (EditText) findViewById(R.id.num_input);et.setText(inContent); } public void in_result(View v){ int count = 0; char c = 0; int pre = 0; int last = 0; while(count < inContent.length()){ if( inContent.charAt(count) == '+'|| inContent.charAt(count) == '-'|| inContent.charAt(count) == '*'|| inContent.charAt(count) == '/' ){ c = inContent.charAt(count); pre = Integer.parseInt(inContent.substring(0, count)); last = Integer.parseInt(inContent.substring(count+1)); } count ++; } int value = 0; switch(c){case'+':value = pre+last;break;case'-':value = pre-last;break;case'*':value = pre*last;break;case'/':value = pre/last;break; } String sss = Integer.toString(value); EditText et = (EditText) findViewById(R.id.num_input); et.setText(sss); }
我的app是这样的:
颜色是测试布局的时候加上去的,后来发现把颜色去掉的话就太沉闷了,所以就不修改了。
测试结果:
相对布局RelativeLayout比LinearLayout要灵活,可以根据前后组件的位置来调动自己的位置。
2 0
- android布局与计算器app编程
- Android计算器界面布局
- Android计算器布局
- Android仿计算器布局代码
- Android开发计算器之布局
- Android画面简单布局-计算器
- 第一个Android app,计算器~
- android App布局
- android App布局
- 用Android仿计算器布局代码
- android线性布局之计算器界面设计
- Android,GridLayout布局(简易计算器)
- android线性布局作业——计算器
- android相对布局--计算器的例子
- Android 一个简单的计算器APP
- 计算器布局
- android学习--adroid线性布局 一个计算器的布局
- App布局模型Android源代码
- Http 分块传输编码 Transfer-Encoding: chunked
- js点击a标签切换不同列表
- android app开发遇到的一些问题(二)
- 对laravel5概念的理解 -- 观察者模式(Event)
- CSS3 2D 转换
- android布局与计算器app编程
- CentOS安装ntfs-3g
- Android 全局异常捕获之CrashHandler
- Linux入门:常用命令:scp上传下载文件
- 博客转移了
- 多示例学习
- VR虚拟现实&AR增强现实编程(2):开发环境Unity3D简介
- C 语言 宏的妙用
- linux进程间通信-信号通信