zoukankan      html  css  js  c++  java
  • andorid 计算器

    avtivity_main.xml

    <?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;
    
            }
        }
    }
  • 相关阅读:
    win10一键访问更改适配器的方法
    windows server 2019 hyper-v+zabbix3.4
    Hyper-V 配置虚拟网络
    ros routeros pppoe一直拨号获取到不一样的IP为止。
    usb3.0 3.1 3.2的区别和联系
    西部数据绿盘、蓝盘、黑盘、红盘和紫盘的区别
    Ros-routeros winbox for win,mac,android,ios客户端大全
    《高效能人士的七个习惯》:好的时间管理,每天只做6件事
    快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
    Kubernetes中分布式存储Rook-Ceph的使用:一个ASP.NET Core MVC的案例
  • 原文地址:https://www.cnblogs.com/cuikang/p/5327589.html
Copyright © 2011-2022 走看看