我使用的是线性布局,也可以使用表格布局,但是暂时还没有学到
首先来看一下布局格式代码,直接将代码写在activity_main中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<EditText
android:id="@+id/re"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@null" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1"/>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:id="@+id/b4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:id="@+id/b6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"/>
<Button
android:id="@+id/b7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6"
android:layout_weight="1"/>
<Button
android:id="@+id/b8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="*"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"
android:layout_weight="1"/>
<Button
android:id="@+id/b10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="8"
android:layout_weight="1"/>
<Button
android:id="@+id/b11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="9"
android:layout_weight="1"/>
<Button
android:id="@+id/b12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="@+id/b13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/b14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="."
android:layout_weight="1"/>
<Button
android:id="@+id/b15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+ "
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/b16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="="/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
得到的格式是这样的:
然后实现它的加减乘除功能,代码写在MainActivity.java中:
package com.newer.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity implements OnClickListener{
EditText re;
Button b1=null;
Button b2=null;
Button b3=null;
Button b4=null;
Button b5=null;
Button b6=null;
Button b7=null;
Button b8=null;
Button b9=null;
Button b10=null;
Button b11=null;
Button b12=null;
Button b13=null;
Button b14=null;
Button b15=null;
Button b16=null;
//声明两个参数。接收re前后的值
double num1=0,num2=0;
double Result=0;//计算结果
int op=0;//判断操作数,
boolean isClickEqu=false;//判断是否按了“=”按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//从布局文件中获取控件
re=(EditText)findViewById(R.id.re);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b4=(Button)findViewById(R.id.b4);
b5=(Button)findViewById(R.id.b5);
b6=(Button)findViewById(R.id.b6);
b7=(Button)findViewById(R.id.b7);
b8=(Button)findViewById(R.id.b8);
b9=(Button)findViewById(R.id.b9);
b10=(Button)findViewById(R.id.b10);
b11=(Button)findViewById(R.id.b11);
b12=(Button)findViewById(R.id.b12);
b13=(Button)findViewById(R.id.b13);
b14=(Button)findViewById(R.id.b14);
b15=(Button)findViewById(R.id.b15);
b16=(Button)findViewById(R.id.b16);
//添加监听\
re.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
b11.setOnClickListener(this);
b12.setOnClickListener(this);
b13.setOnClickListener(this);
b14.setOnClickListener(this);
b15.setOnClickListener(this);
b16.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//btn0--9---------------------------
case R.id.b13:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString=re.getText().toString();
myString+="0";
re.setText(myString);
break;
case R.id.b1:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString1=re.getText().toString();
myString1+="1";
re.setText(myString1);
break;
case R.id.b2:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString2=re.getText().toString();
myString2+="2";
re.setText(myString2);
break;
case R.id.b3:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString3=re.getText().toString();
myString3+="3";
re.setText(myString3);
break;
case R.id.b5:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString4=re.getText().toString();
myString4+="4";
re.setText(myString4);
break;
case R.id.b6:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString5=re.getText().toString();
myString5+="5";
re.setText(myString5);
break;
case R.id.b7:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString6=re.getText().toString();
myString6+="6";
re.setText(myString6);
break;
case R.id.b9:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString7=re.getText().toString();
myString7+="7";
re.setText(myString7);
break;
case R.id.b10:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString8=re.getText().toString();
myString8+="8";
re.setText(myString8);
break;
case R.id.b11:
if(isClickEqu)
{
re.setText(null);
isClickEqu=false;
}
String myString9=re.getText().toString();
myString9+="9";
re.setText(myString9);
break;
//btn+-*/=--------------------------------
case R.id.b15:
String myStringAdd=re.getText().toString();
if(myStringAdd.equals(null))
{
return;
}
num1=Double.valueOf(myStringAdd);
re.setText(null);
op=1;
isClickEqu=false;
break;
case R.id.b12:
String myStringSub=re.getText().toString();
if(myStringSub.equals(null))
{
return;
}
num1=Double.valueOf(myStringSub);
re.setText(null);
op=2;
isClickEqu=false;
break;
case R.id.b8:
String myStringMul=re.getText().toString();
if(myStringMul.equals(null))
{
return;
}
num1=Double.valueOf(myStringMul);
re.setText(null);
op=3;
isClickEqu=false;
break;
case R.id.b4:
String myStringDiv=re.getText().toString();
if(myStringDiv.equals(null))
{
return;
}
num1=Double.valueOf(myStringDiv);
re.setText(null);
op=4;
isClickEqu=false;
break;
case R.id.b16:
String myStringEqu=re.getText().toString();
if(myStringEqu.equals(null))
{
return;
}
num2=Double.valueOf(myStringEqu);
re.setText(null);
switch (op) {
case 0:
Result=num2;
break;
case 1:
Result=num1+num2;
break;
case 2:
Result=num1-num2;
break;
case 3:
Result=num1*num2;
break;
case 4:
Result=num1/num2;
break;
default:
Result=0;
break;
}
re.setText(String.valueOf(Result));
isClickEqu=true;
break;
default:
break;
}
}
}
这里有一点小问题,小数点不可以用,只能做整数操作