zoukankan      html  css  js  c++  java
  • 第四次作业 简易计算器

    要求:

    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 }
  • 相关阅读:
    mysql的sql执行计划
    JMX
    用 Cobertura 测量测试覆盖率
    javap反汇编的使用
    Java ASM 技术简介
    基于Java Instrument的Agent实现
    浮动的同级盒子顶对齐
    子绝父相布局
    相对定位,绝对定位和固定定位
    嵌套的定位盒子如何居中?
  • 原文地址:https://www.cnblogs.com/TSHEN/p/11518780.html
Copyright © 2011-2022 走看看