package com.example.calculator; 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); } }
<?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.23" /> <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.calculator; 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 double i=0,n1,n2,t;//double型 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 = Double.parseDouble(num1.getText().toString()); n2 = Double.parseDouble(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); } }
package com.example.calculator; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private double i=0,n1,n2,t; private Button sum,reduce,ride,except; private EditText num1,num2; private TextView t1; private ProgressBar pb; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler=new Handler(); num1 = findViewById(R.id.nu1); num2 = findViewById(R.id.nu2); t1 = findViewById(R.id.end); sum = findViewById(R.id.sum); reduce = findViewById(R.id.reduce); ride = findViewById(R.id.ride); except = findViewById(R.id.except); sum.setOnClickListener(this); reduce.setOnClickListener(this); ride.setOnClickListener(this); except.setOnClickListener(this); pb = findViewById(R.id.progressBar3); pb.isEnabled(); } @SuppressLint("NewApi") @Override public void onClick(View v){ n1 = Double.parseDouble(num1.getText().toString()); n2 = Double.parseDouble(num2.getText().toString()); switch(v.getId()){ case R.id.sum: pb.setProgress(0,false);//添加进度条 pb.setProgress(100,true); i = n1+n2; break; case R.id.reduce: pb.setProgress(0,false); pb.setProgress(100,true); i = n1-n2; break; case R.id.ride: pb.setProgress(0,false); pb.setProgress(100,true); i = n1*n2; break; case R.id.except: pb.setProgress(0,false); pb.setProgress(100,true); i = n1/n2; break; } String a = String.valueOf(i); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } t1.setText(a); } }