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

    <RelativeLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >    
        <TextView
               android:id="@+id/tv2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/et2"
               android:layout_centerVertical="true"
               android:gravity="center"
               android:text="显示计算结果"
               android:textStyle="italic" />
        <EditText
               android:id="@+id/et3"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_below="@+id/tv2"
               android:layout_alignLeft="@+id/et1"
               android:ems="10" >
        <requestFocus />
    </EditText>   
              <EditText
               android:id="@+id/et1"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_below="@+id/et3"
               android:layout_alignLeft="@+id/tv1"
               android:hint="请输入数值"
               android:ems="10" />
               <EditText
               android:id="@+id/et2"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_below="@+id/et1"
               android:layout_alignLeft="@+id/tv1"
               android:hint="请输入数值"
               android:ems="10" />
         <Button
                android:id="@+id/sum"
                android:layout_width="152dp"
                android:layout_height="wrap_content"
                android:editable="false"
                android:text="+" />
    
            <Button
                android:id="@+id/reduce"
                android:layout_width="152dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/sum"
                android:text="-"
                android:textSize="20sp" />
            <Button
                android:id="@+id/ride"
                android:layout_width="152dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/sum"
                android:text="X" />
    
            <Button
                android:id="@+id/except"
                android:layout_width="152dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/reduce"
                android:layout_toRightOf="@+id/ride"
                android:text="/" />
            
    </RelativeLayout>
    package com.example.aaaa;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void click(View v) {
            TextView tv2 = (TextView) findViewById(R.id.tv2);
            EditText et1 = (EditText) findViewById(R.id.et1);
            EditText et2 = (EditText) findViewById(R.id.et2);
    
            int num1,num2,result=0;
            num1=Integer.parseInt(et1.getText().toString());
            num2=Integer.parseInt(et2.getText().toString());
    
            switch (v.getId()) {
            case R.id.sum:
                result=num1+num2;
                break;
            case R.id.reduce:
                result=num1-num2;
                break;
            case R.id.ride:
                result=num1*num2;
                break;
            case R.id.except:
                result=num1/num2;
                break;
            }
            tv2.setText(result+"");
        }
    
    }
  • 相关阅读:
    zt 五子棋判断禁手的一些资料 学习ing
    acwing 242. 一个简单的整数问题
    acwing 241. 楼兰图腾
    leetcode 1309 Decrypt String from Alphabet to Integer Mapping
    leetcode 1310. XOR Queries of a Subarray
    acwing 1250. 格子游戏 并查集
    MongoDB shell操作
    MongoDB shell操作
    How to retreive raw post data from HttpServletRequest in java
    How to retreive raw post data from HttpServletRequest in java
  • 原文地址:https://www.cnblogs.com/917174759luowenjing/p/13775345.html
Copyright © 2011-2022 走看看