UI代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/love"
>
<TextView
android:id="@+id/edt_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="0"
android:textSize="40sp"
android:gravity="right"
android:textColor="#ffffff"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:textSize="30sp"
/>
<Button
android:id="@+id/clear"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="删除"
android:layout_weight="1"
android:textColor="#ffffff"
android:textSize="25dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/b7"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="7"
android:textColor="#ffffff"
android:layout_weight="1"
android:textSize="50dp"
/>
<Button
android:id="@+id/b8"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="8"
android:layout_weight="1"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b9"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="9"
android:textColor="#ffffff"
android:layout_weight="1"
android:textSize="50dp"
/>
<Button
android:id="@+id/divide"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="/"
android:textColor="#ffffff"
android:layout_weight="1"
android:textSize="50dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/b4"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b5"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b6"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/mulply"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="X"
android:textColor="#ffffff"
android:textSize="50dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/b1"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b2"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b3"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/reduce"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textColor="#ffffff"
android:textSize="50dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/point"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/b0"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/equal"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:textColor="#ffffff"
android:textSize="50dp"
/>
<Button
android:id="@+id/add"
android:onClick="onClick"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textColor="#ffffff"
android:textSize="50dp"
/>
</LinearLayout>
</LinearLayout>
java代码如下:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button add;
private Button clear;
private Button divide;
private Button mulply;
private Button equal;
private Button point;
private Button reduce;
private TextView edt_01;
private EditText edt_02;
char c=' ';//运算符号
double firNum=0;//第一个数
double secNum=0;//第一个数
boolean dot=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.add);
clear = (Button) findViewById(R.id.clear);//删除
divide = (Button) findViewById(R.id.divide);//除
reduce = (Button) findViewById(R.id.reduce);//减
reduce = (Button) findViewById(R.id.mulply);//乘
equal = (Button) findViewById(R.id.equal);
point = (Button) findViewById(R.id.point);
edt_01 = (TextView) findViewById(R.id.edt_01);
edt_02 = (EditText) findViewById(R.id.edt_02);
}
public void onClick(View v){
if(v.getId()==(R.id.b0)){
edt_02.setText(edt_02.getText()+"0");
}
if(v.getId()==(R.id.b1)){
edt_02.setText(edt_02.getText()+"1");
}
if(v.getId()==(R.id.b2)){
edt_02.setText(edt_02.getText()+"2");
}
if(v.getId()==(R.id.b3)){
edt_02.setText(edt_02.getText()+"3");
}
if(v.getId()==(R.id.b4)){
edt_02.setText(edt_02.getText()+"4");
}
if(v.getId()==(R.id.b5)){
edt_02.setText(edt_02.getText()+"5");
}
if(v.getId()==(R.id.b6)){
edt_02.setText(edt_02.getText()+"6");
}
if(v.getId()==(R.id.b7)){
edt_02.setText(edt_02.getText()+"7");
}
if(v.getId()==(R.id.b8)){
edt_02.setText(edt_02.getText()+"8");
}
if(v.getId()==(R.id.b9)){
edt_02.setText(edt_02.getText()+"9");
}
if(v.getId()==(R.id.clear)){
edt_02.setText("");
edt_01.setText("");
}
if(edt_02.getText().toString().equals("")){//如果editText为空
Toast.makeText(this, "没有输入数字", Toast.LENGTH_SHORT).show();
}
if(v.getId()==(R.id.add)){
if(!edt_02.getText().toString().equals("")){
c='+';
firNum=Double.parseDouble(edt_02.getText().toString());
edt_02.setText("");
}else{
Toast.makeText(getApplicationContext(), "没有输入数字怎么能计算呢", 0).show();
}
}
if(v.getId()==(R.id.divide)){
c='/';
if(!edt_02.getText().toString().equals("")){
firNum=Double.parseDouble(edt_02.getText().toString());
edt_02.setText("");
}else{
Toast.makeText(getApplicationContext(), "没有输入数字怎么能计算呢", 0).show();
}
}
if(v.getId()==(R.id.reduce)){
c='-';
if(!edt_02.getText().toString().equals("")){
firNum=Double.parseDouble(edt_02.getText().toString());
edt_02.setText("");
}else{
Toast.makeText(getApplicationContext(), "没有输入数字怎么能计算呢", 0).show();
}
Log.i("tag", edt_02.getText().toString());
}
if(v.getId()==(R.id.mulply)){
c='*';
if(!edt_02.getText().toString().equals("")){
firNum=Double.parseDouble(edt_02.getText().toString());
edt_02.setText("");
}else{
Toast.makeText(getApplicationContext(), "没有输入数字怎么能计算呢", 0).show();
}
}
if(v.getId()==(R.id.point)){
dot=false;
edt_02.setText(edt_02.getText()+".");
//edt_02.setText("");
}
if(edt_02.getText().toString().equals("")){
return;
}else{
}
if(v.getId()==(R.id.equal)){
double result=0;
secNum=Double.parseDouble(edt_02.getText().toString());
if(c=='+'){
result=firNum+secNum;
}
if(c=='-'){
result=firNum-secNum;
}
if(c=='*'){
result=firNum*secNum;
}
if(c=='/'){
if(secNum==0){
Toast.makeText(this, "被除数不可以为0", Toast.LENGTH_SHORT).show();
edt_02.setText("");
}else{
result=firNum/secNum;
}
}
edt_01.setText(result+"");
}
}
}
此程序只是实现了简单的加减乘除功能。
有不够完善,有待改善。