安卓简易计算器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00BCD4"
        android:editable="true"
        android:gravity="center"
        android:text="Calculator"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="true"
        android:ems="10"
        android:gravity="right"
        android:inputType="time"
        android:text="2020.9.27" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="true"
        android:text="第一个数" />

    <EditText
        android:id="@+id/nu1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:numeric="integer"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="true"
        android:text="第二个数" />

    <EditText
        android:id="@+id/nu2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:numeric="integer" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <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:text="-"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ride"
            android:layout_width="152dp"
            android:layout_height="wrap_content"
            android:text="X" />

        <Button
            android:id="@+id/except"
            android:layout_width="152dp"
            android:layout_height="wrap_content"
            android:text="/" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="true"
        android:text="结果" />

    <TextView
        android:id="@+id/end"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

</LinearLayout>
package com.example.myhomework;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int i=0,n1,n2,t;
    private Button sum,reduce,ride,except;
    private EditText num1,num2;
    private TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        num1 = (EditText)findViewById(R.id.nu1);
        num2 = (EditText)findViewById(R.id.nu2);
        t1 = (TextView)findViewById(R.id.end);
        sum = (Button)findViewById(R.id.sum);
        reduce = (Button)findViewById(R.id.reduce);
        ride = (Button)findViewById(R.id.ride);
        except = (Button)findViewById(R.id.except);
        sum.setOnClickListener(this);
        reduce.setOnClickListener(this);
        ride.setOnClickListener(this);
        except.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        n1=Integer.parseInt(num1.getText().toString());
        n2=Integer.parseInt(num2.getText().toString());
        switch(v.getId()){
            case R.id.sum:
                i = n1+n2;
                break;
            case R.id.reduce:
                i = n1-n2;
                break;
            case R.id.ride:
                i = n1*n2;
                break;
            case R.id.except:
                i = n1/n2;
                break;
        }
        String a = String.valueOf(i);
        t1.setText(a);
    }
}

 

posted @ 2020-12-07 15:30  wxy816  阅读(58)  评论(0编辑  收藏  举报