第四次作业 简易计算器
要求:
1.输入两个数,当点击单选钮(加减乘除)的时候,在下面的TextView中显示对应的计算结果。
2.button按钮是清空,作用是清空两个edittext和TextView中的内容。
3.需要提交layout代码和activity的代码。此外还需要运行截图。
4.请最晚于下周二晚上8点之前完成。之前3次作业没有完成的请按时补交。
5.如对作业有疑问,请在下面留言。祝各位同学中秋节快乐!!
实现效果图:
简单布局code
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity" 9 android:background="#03A9F4"> 10 <LinearLayout 11 android:id="@+id/line1" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:orientation="horizontal" 15 android:layout_marginTop="150dp" 16 android:layout_marginLeft="30sp" 17 android:layout_marginRight="30sp" 18 android:background="#E9E0E0"> 19 20 21 <TextView 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="请输入第一个数:" 25 android:textSize="20sp" 26 /> 27 <EditText 28 android:id="@+id/one" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:background="#FFFFFF" 32 android:numeric="integer" 33 android:text="0" 34 /> 35 </LinearLayout> 36 37 <LinearLayout 38 android:id="@+id/line2" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" 41 android:layout_below="@+id/line1" 42 android:layout_marginTop="40dp" 43 android:layout_marginLeft="30sp" 44 android:layout_marginRight="30sp" 45 android:orientation="horizontal"> 46 47 48 <TextView 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="请输入第二个数:" 52 android:textSize="20sp" 53 android:background="#E7DFDF" 54 55 /> 56 <EditText 57 android:id="@+id/two" 58 android:layout_width="match_parent" 59 android:layout_height="wrap_content" 60 android:textSize="20sp" 61 android:background="#FFFFFF" 62 android:numeric="integer" 63 android:text="0"/> 64 </LinearLayout> 65 66 <RadioGroup 67 android:id="@+id/rg" 68 android:layout_width="match_parent" 69 android:layout_height="wrap_content" 70 android:layout_below="@+id/line2" 71 android:layout_marginTop="40dp" 72 android:background="#EEEEEE" 73 android:orientation="horizontal"> 74 <RadioButton 75 android:id="@+id/jia" 76 android:layout_width="wrap_content" 77 android:layout_height="wrap_content" 78 android:text="+" 79 android:layout_weight="1" 80 android:textSize="30sp" 81 /> 82 <RadioButton 83 android:id="@+id/jian" 84 android:layout_width="wrap_content" 85 android:layout_height="wrap_content" 86 android:text="--" 87 android:layout_weight="1" 88 android:textSize="30sp" 89 /> 90 <RadioButton 91 android:id="@+id/ch" 92 android:layout_width="wrap_content" 93 android:layout_height="wrap_content" 94 android:text="X" 95 android:layout_weight="1" 96 android:textSize="30sp" 97 /> 98 <RadioButton 99 android:id="@+id/chu" 100 android:layout_width="wrap_content" 101 android:layout_height="wrap_content" 102 android:text="/" 103 android:layout_weight="1" 104 android:textSize="30sp" 105 /> 106 </RadioGroup> 107 <TextView 108 android:id="@+id/jg" 109 android:layout_width="match_parent" 110 android:layout_height="60dp" 111 android:layout_marginLeft="30sp" 112 android:layout_marginRight="30sp" 113 android:layout_below="@+id/rg" 114 android:layout_marginTop="40dp" 115 android:background="#EEEEEE" 116 /> 117 <Button 118 android:id="@+id/qk" 119 android:layout_width="wrap_content" 120 android:layout_height="wrap_content" 121 android:text="清空" 122 android:textSize="30sp" 123 android:background="#F44336" 124 android:layout_below="@+id/jg" 125 android:layout_centerHorizontal="true" 126 android:layout_marginTop="30sp" 127 android:onClick="qkz" 128 /> 129 </RelativeLayout>
简单实现Java代码:
1 package com.example.ft; 2 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.EditText; 6 import android.widget.RadioGroup; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 import androidx.appcompat.app.AppCompatActivity; 11 public class MainActivity extends AppCompatActivity { 12 public RadioGroup radioGroup; 13 EditText one1; 14 EditText two1; 15 TextView jg; 16 Integer d1; 17 Integer d2; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 radioGroup=findViewById(R.id.rg); 23 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 24 @Override 25 public void onCheckedChanged(RadioGroup radioGroup, int i) { 26 switch (i){ 27 case R.id.jia:jia();break; 28 case R.id.jian:jian();break; 29 case R.id.ch:ch();break; 30 case R.id.chu:chu();break; 31 32 } 33 34 } 35 }); 36 } 37 38 private void chu() { 39 jg= findViewById(R.id.jg); 40 one1= findViewById(R.id.one); 41 two1= findViewById(R.id.two); 42 String on=one1.getText().toString(); 43 String tw=two1.getText().toString(); 44 d1=Integer.valueOf(on); 45 d2=Integer.valueOf(tw); 46 if (d2==0||d1==0){ 47 Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show(); 48 }else { 49 String st = Integer.toString(d1/d2); 50 jg.setText(st); 51 } 52 } 53 54 private void ch() { 55 jg= findViewById(R.id.jg); 56 one1= findViewById(R.id.one); 57 two1= findViewById(R.id.two); 58 String on=one1.getText().toString(); 59 String tw=two1.getText().toString(); 60 d1=Integer.valueOf(on); 61 d2=Integer.valueOf(tw); 62 String st = Integer.toString(d2*d1); 63 jg.setText(st); 64 } 65 66 private void jian() { 67 jg= findViewById(R.id.jg); 68 one1= findViewById(R.id.one); 69 two1= findViewById(R.id.two); 70 String on=one1.getText().toString(); 71 String tw=two1.getText().toString(); 72 d1=Integer.valueOf(on); 73 d2=Integer.valueOf(tw); 74 String st = Integer.toString(d1-d2); 75 jg.setText(st); 76 } 77 78 private void jia() { 79 jg= findViewById(R.id.jg); 80 one1= findViewById(R.id.one); 81 two1= findViewById(R.id.two); 82 String on=one1.getText().toString(); 83 String tw=two1.getText().toString(); 84 d1=Integer.valueOf(on); 85 d2=Integer.valueOf(tw); 86 String st = Integer.toString(d2+d1); 87 jg.setText(st); 88 89 } 90 91 public void qkz(View view) { 92 jg.setText(""); 93 one1.setText(""); 94 two1.setText(""); 95 } 96 97 98 }
转载于:https://www.cnblogs.com/TSHEN/p/11518780.html
第四次作业 简易计算器相关推荐
- android studio大作业-简易计算器实现
android studio大作业-简易计算器实现 先看效果图 基本功能:加,减,乘,除 核心代码实现 public class MainActivity extends AppCompatActiv ...
- JAVA作业-简易计算器
工程名随意,文件名必须为Jisuanqi.java View Code import java.awt.BorderLayout; import java.awt.Color; import java ...
- pink老师作业 简易计算器
do { var num=prompt('欢迎使用计算器:\n' +'1.加法运算:\n'+'2.减法运算:\n'+'3.乘法运算:\n'+'4.除法运算:\n'+'5.退出:\n'+'请输入您的选项 ...
- 简易计算器 (C语言)作业
从Github下载 从CSDN下载 // // ViewController.h // SimpleCalculator // // Created by Mewlan Musajan on 11/2 ...
- codeblocks 51单片机学习(四)简易计算器
基于51单片机的数码管和矩阵按键实现的简易计算器,其实可以加入的东西有很多,就像我这个负数运算还没有加进去,以及我的除法运算只能算655(65535/100)以内的数,不过就先做到这里,这个作品至少已 ...
- Android入门之简易计算器(一)
Android Android入门之简易计算器(一) 文章目录 Android Android入门之简易计算器(一) @[TOC](文章目录) 前言 一.项目结构 二.界面显示 三.前端界面 1.界面 ...
- QT入门项目--简易计算器
QT入门项目–简易计算器 目录 QT入门项目--简易计算器 一.前言 二.运行界面 三.代码 dialog.cpp doexpr.cpp check.cpp 四.总结 一.前言 C语言快要结课了,自然 ...
- android简易计算器(两位数的加减乘除求余)
该项目是两年前刚学android时,课堂上老师布置的一个作业, 要求是:能够实现两位数(正数或负数)的加减乘除以及求余的功能. 一.简易计算器界面展示 1.两位数的加法 2.两位数的减法 3.两位 ...
- 怎么用python自制计算公式_手把手教你用python制作简易计算器,能够记录你使用的情况...
话不多说,首先先看效果图,它能够记录你在使用过程中的历史,方便你查看是否有错: 接下来就仔细分析一下是如何制作的: 简易计算器 第一步:导入资源库 在过程中使用到了tkinter这个资源库,win+R ...
最新文章
- 【Python算法】哈希存储、哈希表、散列表原理
- Server.Transfer 和 Response.Redirect 的用法
- html期末网页设计,求网页设计的期末作业一份 HTML的
- std::map的insert和下标[]访问
- Deep Learning of Binary Hash Codes for Fast Image Retrieval(2015)
- ibm服务器做系统视频,智慧运算 IBM x3650 M4服务器拆机(视频)
- ZOJ 3511 Cake Robbery
- Atitit 指令集(IA及指令集架构 1. 指令集(IA:InstructionSet)是指CPU指令系统所能识别(翻译)执行的全部指令的集合。	1 1.1. (1)运算指令	1 1.2. (2)
- SQL注入漏洞-POST注入
- linux 安装protoc
- 51单片机红外遥控小车
- python下dicom格式图像转化为jpg格式图像
- 电商项目(谷粒商城)
- 逻辑函数的两种标准形式
- Python应用之批量打水印
- R语言并行计算 deviation of null beta diversity(beta多样性零偏差)
- dram sram drom srom ddram详细解释
- 最赏识王小川的,还是马化腾
- 服务攻防——数据库安全之未授权访问、弱口令
- 计算机教育格言,苏霍姆林斯基教育名言大全