第四次作业

要求:

1.输入两个数,当点击单选钮(加减乘除)的时候,在下面的TextView中显示对应的计算结果。

2.button按钮是清空,作用是清空两个edittext和TextView中的内容。

3.需要提交layout代码和activity的代码。此外还需要运行截图。

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#03A9F4">
    <LinearLayout
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="30sp"
        android:layout_marginRight="30sp"
        android:background="#E9E0E0">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入第一个数:"
            android:textSize="20sp"
            />
        <EditText
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:numeric="integer"
            android:text="0"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/line2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/line1"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="30sp"
        android:layout_marginRight="30sp"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入第二个数:"
            android:textSize="20sp"
            android:background="#E7DFDF"

            />
        <EditText
            android:id="@+id/two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:background="#FFFFFF"
            android:numeric="integer"
            android:text="0"/>
    </LinearLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/line2"
        android:layout_marginTop="40dp"
        android:background="#EEEEEE"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/jia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:textSize="30sp"
            />
        <RadioButton
            android:id="@+id/jian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="--"
            android:layout_weight="1"
            android:textSize="30sp"
            />
        <RadioButton
            android:id="@+id/ch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            android:layout_weight="1"
            android:textSize="30sp"
            />
        <RadioButton
            android:id="@+id/chu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_weight="1"
            android:textSize="30sp"
            />
    </RadioGroup>
    <TextView
        android:id="@+id/jg"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="30sp"
        android:layout_marginRight="30sp"
        android:layout_below="@+id/rg"
        android:layout_marginTop="40dp"
        android:background="#EEEEEE"
        />
    <Button
        android:id="@+id/qk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空"
        android:textSize="30sp"
        android:background="#F44336"
        android:layout_below="@+id/jg"
 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 }
 
  

 


        android:layout_centerHorizontal="true"
        android:layout_marginTop="30sp"
        android:onClick="qkz"
        />
</RelativeLayout>

 

转载于:https://www.cnblogs.com/zz2333/p/11574078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值