布局太简单了,就直接给代码了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:gravity="center">
<EditText
android:id="@+id/edt_num1"
android:layout_width="60dp"
android:layout_height="80dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="50dp"
android:layout_height="80dp"
android:entries="@array/songs">
</Spinner>
<EditText
android:id="@+id/edt_num2"
android:layout_width="60dp"
android:layout_height="80dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="30sp"/>
<EditText
android:id="@+id/edt_result"
android:layout_width="80dp"
android:layout_height="80dp"
android:enabled="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"/>
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除"/>
<Button
android:id="@+id/btn_quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
</LinearLayout>
</LinearLayout>
主窗口类:
步骤:
1、初始化控件
2、下拉列表(spinner)使用setOnItemSelectedListner类可以知道用户选中的下拉列表中的哪一项。
3、完善单击计算按钮触发的按钮单击事件处理,获得两个文本框中输入的字符串,判断是否为空判断下拉列表的选中参数,根据不同的字符调用不同的计算方法。防止可能定义出安卓的保留字或者关键字,所以每个方法名都由字母+1表示。
退出和清空方法:
主窗口代码:
package com.example.a15114.fouroperations;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
EditText edt_num1;
EditText edt_num2;
EditText edt_result;
Button count;
Button clear;
Button quit;
Spinner spinner;
String sign;
String num1;
String num2;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_num1=findViewById(R.id.edt_num1);
edt_num2=findViewById(R.id.edt_num2);
edt_result=findViewById(R.id.edt_result);
count=findViewById(R.id.btn_count);
clear=findViewById(R.id.btn_clear);
quit=findViewById(R.id.btn_quit);
spinner=findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sign=spinner.getSelectedItem().toString();
System.out.println(""+sign);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=edt_num1.getText().toString().trim();
num2=edt_num2.getText().toString().trim();
if ((!num1.equals(""))&&(!num2.equals(""))){
int firstnumber=Integer.parseInt(num1);
int secondnumber=Integer.parseInt(num2);
int resultint=-1;
if (sign.equals("+")){
add1(firstnumber,secondnumber);
}else if (sign.equals("-")){
minus(firstnumber,secondnumber);
}else if (sign.equals("*")){
time1(firstnumber,secondnumber);
}else if (sign.equals("/")){
divide1(firstnumber,secondnumber);
}
}else {
Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edt_num1.setText("");
edt_num2.setText("");
edt_result.setText("");
}
});
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void add1(int num1,int num2){
//加法运算
int resultint=-1;
resultint=num1+num2;
edt_result.setText(resultint+"");
}
public void minus(int num1,int num2){
//减法运算
int resultint=-1;
resultint=num1-num2;
edt_result.setText(resultint+"");
}
public void time1(int num1,int num2){
//乘法运算
int resultint=-1;
resultint=num1*num2;
edt_result.setText(resultint+"");
}
public void divide1(int num1,int num2){
//除法运算
int resultint=-1;
if (num2==0){
edt_result.setText("");
Toast.makeText(MainActivity.this,"被除数不能为0",Toast.LENGTH_SHORT).show();
}else {
resultint=num1/num2;
edt_result.setText(resultint+"");
}
}
}