andorid 计算器
2016-03-28 01:25
260 查看
avtivity_main.xml
CalculatorActivity
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" > <!--rowCount 行数 columnCount 列数 layout_columnSpan 跨列 layout_rowSpan 跨行 fill_horizontal 水平填充 fill_vertical 垂直填充 layout_columnWeight 权重列 layout_rowWeight 权重行 background @drawable/ 设置图片 layout_row 指定行号 layout_column 指定列好 从0开始 --> <EditText android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:layout_columnWeight="1" android:layout_rowWeight="1" android:editable="false" android:gravity="right|center_vertical" android:id="@+id/et"/> <Button android:text="清除" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" android:id="@+id/clear" /> <Button android:text="后退" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" android:id="@+id/goback"/> <Button android:text="/" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="X" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="7" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" android:id="@+id/bt_7"/> <Button android:text="8" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" android:id="@+id/bt_8"/> <Button android:text="9" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" android:id="@+id/bt_9"/> <Button android:text="-" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="4" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="5" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="6" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="+" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="1" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="2" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="3" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp"/> <Button android:text="=" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" android:layout_columnWeight="1" android:layout_rowWeight="2" android:textSize="25sp" android:background="#24f" /> <Button android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:layout_columnWeight="2" android:layout_rowWeight="1" android:textSize="25sp" /> <Button android:text="." android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="25sp" /> </GridLayout>
CalculatorActivity
package com.hanqi.application3; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * Created by Administrator on 2016/3/28. */ public class CalculatorActivity extends Activity implements View.OnClickListener { EditText et; Button bt_clear; Button bt_goback; Button bt_7; Button bt_8; Button bt_9; //构建字符串StringBuffer //存储显示的内容 private StringBuffer str_show= new StringBuffer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.avtivity_main); et=(EditText)findViewById(R.id.et); bt_clear=(Button)findViewById(R.id.clear); bt_7=(Button)findViewById(R.id.bt_7); bt_8=(Button)findViewById(R.id.bt_8); bt_9=(Button)findViewById(R.id.bt_9); bt_clear.setOnClickListener(this); bt_goback.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); } public void onClick(View v) { Button bt = (Button)v; int id =bt.getId(); switch (id) { case R.id.clear: str_show= new StringBuffer(); et.setText(str_show); break; case R.id.goback: str_show.codePointAt(str_show.length()-1); et.setText(str_show); break; case R.id.bt_7: case R.id.bt_8: case R.id.bt_9: str_show.append(bt.getText()); et.setText(str_show); break; } } }
相关文章推荐
- andorid UI事件 监听器
- 修改linux的防火墙
- Oracle安装错误ora-00922(缺少或无效选项)
- Nginx学习笔记:对正向服务器与反向服务器的理解
- 为云量身定制您的服务
- andorid UI事件
- 深入理解CSS中的空白符和换行
- Android Studio发布到Jcenter
- andorid EditView
- Python语言编写用多行Unicode字符串表示下面的唐诗’静夜思‘并打印
- 【JavaScript】ECMAScript5中新增的数组操作函数
- Java注解浅析
- hdu2818 并查集
- C++实验二—分段函数求值
- IE edge是怎么了??
- ElasticSearch之动态映射和模板
- 对Activity的封装
- 习题2-3:倒三角
- Memcached 及 Redis 架构分析和比较
- Java文档注释生成说明书