The fourth operation simple calculator

Claim:

1. The two input numbers, click a radio button (addition, subtraction) when displaying the calculation result in the following corresponding in TextView.

2.button button is empty, it is to empty the contents of two edittext and TextView.

3. The need to submit layout code and activity code. It also needs to be run shots.

4. Please be completed by next Tuesday at the latest 20:00. Three times before the job is not completed on time, please pay.

5. If you have questions about the job, please leave a message below. I wish you a happy Mid-Autumn Festival students! !

Achieve renderings:

 

Simple layout 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>

Simple implementation of Java code:

 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 }

 

Guess you like

Origin www.cnblogs.com/TSHEN/p/11518780.html