欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android项目编写计算器 博客分类: Android UI 界面 移动终端 android 基础

程序员文章站 2024-03-14 22:35:41
...

 

效果:


android项目编写计算器
            
    
    博客分类: Android UI 界面 移动终端 android 基础
 

 

步骤

  1、布局方式:网格布局(GridLayout)

6行4列,以、二两行是一行一列,其余四行是一行四列。

得到xml文件

 

 

main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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" >
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
>
<EditText
android:id="@+id/tvResult"
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"/>
<Button
android:id="@+id/btclean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="4"
android:text="清除"
/>
<Button
android:id="@+id/bt7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:text="7"
/>
<Button
android:id="@+id/bt8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:text="8"
/>
<Button
android:id="@+id/bt9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="2"
android:text="9"
/>
<Button
android:id="@+id/btDive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="3"
android:text="%"
android:layout_gravity="fill_horizontal"
/>
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:text="4"
/>
<Button
android:id="@+id/bt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="1"
android:text="5"
/>
<Button
android:id="@+id/bt6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="2"
android:text="6"
/>
<Button
android:id="@+id/btMulti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="3"
android:text="*"
android:layout_gravity="fill_horizontal"
/>

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:text="1"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="1"
android:text="2"
/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="2"
android:text="3"
/>
<Button
android:id="@+id/btMinu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="3"
android:text="-"
android:layout_gravity="fill_horizontal"
/>
<Button
android:id="@+id/bt_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="5"
android:layout_column="0"
android:text="."
/>
<Button
android:id="@+id/bt0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="5"
android:layout_column="1"
android:text="0"
/>
<Button
android:id="@+id/btEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="5"
android:layout_column="2"
android:text="="
/>
<Button
android:id="@+id/btAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="5"
android:layout_column="3"
android:text="+"
android:layout_gravity="fill_horizontal"
/>

</GridLayout>


</RelativeLayout>

 demin.xml

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>

 

 

 

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);

}
});

}
}

 

  • android项目编写计算器
            
    
    博客分类: Android UI 界面 移动终端 android 基础
  • 大小: 427.4 KB