效果:
步骤
1、布局方式:网格布局(GridLayout)
6行4列,以、二两行是一行一列,其余四行是一行四列。
得到xml文件
main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="4"
android:textSize="25dp"
android:gravity="right"/>
demin.xml
16dp
16dp
2、给button按钮注册监听器,三种类别:a,数字和小数点;b,加减乘除;c,等于号
Activity.java
package android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Laborary2Activity extends Activity {
/** Called when the activity is first created. */
EditText displayText=null;
double num1=0,num2=0,result=0;
int calType=0;
boolean flag=false;
Button bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt_;
Button btEqual,btAdd,btMinu,btMulti,btDive,btclean;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
bt0=(Button)findViewById(R.id.bt0);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt3=(Button)findViewById(R.id.bt3);
bt4=(Button)findViewById(R.id.bt4);
bt5=(Button)findViewById(R.id.bt5);
bt6=(Button)findViewById(R.id.bt6);
bt7=(Button)findViewById(R.id.bt7);
bt8=(Button)findViewById(R.id.bt8);
bt9=(Button)findViewById(R.id.bt9);
bt_=(Button)findViewById(R.id.bt_);
btEqual=(Button)findViewById(R.id.btEqual);
btAdd=(Button)findViewById(R.id.btAdd);
btMinu=(Button)findViewById(R.id.btMinu);
btMulti=(Button)findViewById(R.id.btMulti);
btDive=(Button)findViewById(R.id.btDive);
displayText=(EditText)findViewById(R.id.tvResult);
btclean=(Button)findViewById(R.id.btclean);
btclean.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayText.setText(null);
}
});
btAdd.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被加数", Toast.LENGTH_LONG).show();
}
else{
num1=Double.parseDouble(myString);
calType=1;
displayText.setText(null);
}
}});
btMinu.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被减数", Toast.LENGTH_LONG).show();
}else {num1=Double.parseDouble(myString);
calType=2;
displayText.setText(null);
}
}});
btMulti.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入乘数", Toast.LENGTH_LONG).show();
}
else
{
num1=Double.parseDouble(myString);
calType=3;
displayText.setText(null);
}
}});
btDive.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被除数", Toast.LENGTH_LONG).show();
}
else
{num1=Double.parseDouble(myString);
calType=4;
displayText.setText(null);
}
}});
btEqual.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "你要计算什么?", Toast.LENGTH_LONG).show();
}
else{
num2=Double.parseDouble(myString);
switch(calType){
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;
}
}
String tempResult=String.valueOf(result);
displayText.setText(tempResult);
flag=true;
}
}});
bt_.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+".";
displayText.setText(myString);
}
});
bt9.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"9";
displayText.setText(myString);
}
});
bt8.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"8";
displayText.setText(myString);
}
});
bt7.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"7";
displayText.setText(myString);
}
});
bt6.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"6";
displayText.setText(myString);
}
});
bt5.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"5";
displayText.setText(myString);
}
});
bt4.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"4";
displayText.setText(myString);
}
});
bt3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"3";
displayText.setText(myString);
}
});
bt2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"2";
displayText.setText(myString);
}
});
bt1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"1";
displayText.setText(myString);
}
});
bt0.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"0";
displayText.setText(myString);
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Laborary2Activity extends Activity {
/** Called when the activity is first created. */
EditText displayText=null;
double num1=0,num2=0,result=0;
int calType=0;
boolean flag=false;
Button bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt_;
Button btEqual,btAdd,btMinu,btMulti,btDive,btclean;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
bt0=(Button)findViewById(R.id.bt0);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt3=(Button)findViewById(R.id.bt3);
bt4=(Button)findViewById(R.id.bt4);
bt5=(Button)findViewById(R.id.bt5);
bt6=(Button)findViewById(R.id.bt6);
bt7=(Button)findViewById(R.id.bt7);
bt8=(Button)findViewById(R.id.bt8);
bt9=(Button)findViewById(R.id.bt9);
bt_=(Button)findViewById(R.id.bt_);
btEqual=(Button)findViewById(R.id.btEqual);
btAdd=(Button)findViewById(R.id.btAdd);
btMinu=(Button)findViewById(R.id.btMinu);
btMulti=(Button)findViewById(R.id.btMulti);
btDive=(Button)findViewById(R.id.btDive);
displayText=(EditText)findViewById(R.id.tvResult);
btclean=(Button)findViewById(R.id.btclean);
btclean.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayText.setText(null);
}
});
btAdd.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被加数", Toast.LENGTH_LONG).show();
}
else{
num1=Double.parseDouble(myString);
calType=1;
displayText.setText(null);
}
}});
btMinu.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被减数", Toast.LENGTH_LONG).show();
}else {num1=Double.parseDouble(myString);
calType=2;
displayText.setText(null);
}
}});
btMulti.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入乘数", Toast.LENGTH_LONG).show();
}
else
{
num1=Double.parseDouble(myString);
calType=3;
displayText.setText(null);
}
}});
btDive.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))
return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "请输入被除数", Toast.LENGTH_LONG).show();
}
else
{num1=Double.parseDouble(myString);
calType=4;
displayText.setText(null);
}
}});
btEqual.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CharSequence temp=displayText.getText();
if(temp.equals(null))return;
String myString=temp.toString();
if(myString.equals("")||myString==null){
Toast.makeText(Laborary2Activity.this, "你要计算什么?", Toast.LENGTH_LONG).show();
}
else{
num2=Double.parseDouble(myString);
switch(calType){
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;
}
}
String tempResult=String.valueOf(result);
displayText.setText(tempResult);
flag=true;
}
}});
bt_.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+".";
displayText.setText(myString);
}
});
bt9.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"9";
displayText.setText(myString);
}
});
bt8.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"8";
displayText.setText(myString);
}
});
bt7.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"7";
displayText.setText(myString);
}
});
bt6.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"6";
displayText.setText(myString);
}
});
bt5.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"5";
displayText.setText(myString);
}
});
bt4.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"4";
displayText.setText(myString);
}
});
bt3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"3";
displayText.setText(myString);
}
});
bt2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"2";
displayText.setText(myString);
}
});
bt1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"1";
displayText.setText(myString);
}
});
bt0.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
displayText.setText(null);
flag=false;
}
CharSequence temp=displayText.getText();
String myString=temp.toString();
myString=myString+"0";
displayText.setText(myString);
}
});
}
}