我的Android之旅——简易计算机

一个适合初学者的简易计算器。

设计计算器,首先想到的是布局,以手机自带的计算器为例子,界面布局文件具体代码如下:


<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"
tools:context=".MainActivity"
android:background="#000"
>

<!-- 外层布局总想排列 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- 第一行的文本框 -->
<TextView
android:id="@+id/txtNumber"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:background="#333"
android:textColor="#fff"
android:textSize="80sp"
android:text=""
android:gravity="right"/>

<!-- 第二行的清除按钮 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/btnClier"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="1sp"
android:textColor="#fff"
android:textSize="50sp"
android:text="清空"/>
<Button
android:id="@+id/btnReturn"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="1sp"
android:textColor="#fff"
android:textSize="50sp"
android:text="←"/>

</LinearLayout>

<!-- 其他按钮 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/btn7"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="7"/>
<Button
android:id="@+id/btn8"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="8"/>
<Button
android:id="@+id/btn9"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="9"/>
<Button
android:id="@+id/btnChu"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="÷"/>

</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/btn4"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="4"/>
<Button
android:id="@+id/btn5"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="5"/>
<Button
android:id="@+id/btn6"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="6"/>
<Button
android:id="@+id/btnCheng"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="*"/>

</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="1"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="2"/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="3"/>
<Button
android:id="@+id/btnJian"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="-"/>

</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/btnDian"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="."/>
<Button
android:id="@+id/btn0"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="0"/>
<Button
android:id="@+id/btnDeng"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="="/>
<Button
android:id="@+id/btnJia"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_margin="1sp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="50sp"
android:text="+"/>

</LinearLayout>




</LinearLayout>


</RelativeLayout>


这里需要注意,Weight属性是设置屏幕界面平均分布给各个空间的。

具体的实现代码:

package com.example.test0708_01;

import com.example.test0708_01.R.string;
import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

String number1;
String number2;
String number3;
String operator="";
boolean isDian=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final TextView txt_Number=(TextView) findViewById(R.id.txtNumber);

final Button btn_0=(Button) findViewById(R.id.btn0);
final Button btn_1=(Button) findViewById(R.id.btn1);
final Button btn_2=(Button) findViewById(R.id.btn2);
final Button btn_3=(Button) findViewById(R.id.btn3);
final Button btn_4=(Button) findViewById(R.id.btn4);
final Button btn_5=(Button) findViewById(R.id.btn5);
final Button btn_6=(Button) findViewById(R.id.btn6);
final Button btn_7=(Button) findViewById(R.id.btn7);
final Button btn_8=(Button) findViewById(R.id.btn8);
final Button btn_9=(Button) findViewById(R.id.btn9);

//数字按钮
final Button[] btn_Number={btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9};
//按钮 点
final Button btn_Dian=(Button) findViewById(R.id.btnDian);
//等号
final Button btn_Deng=(Button) findViewById(R.id.btnDeng);

final Button btn_Jia=(Button) findViewById(R.id.btnJia);
final Button btn_Jian=(Button) findViewById(R.id.btnJian);
final Button btn_Cheng=(Button) findViewById(R.id.btnCheng);
final Button btn_Chu=(Button) findViewById(R.id.btnChu);
//运算符号
final Button[] btn_Operator={btn_Jia,btn_Jian,btn_Cheng,btn_Chu};
//清除按钮
final Button btn_Return=(Button) findViewById(R.id.btnReturn);
final Button btn_Clear=(Button) findViewById(R.id.btnClier);


// btn_0.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View arg0) {
// // TODO 自动生成的方法存根
// txt_Number.setText("00000000");
// }
// });
//实现数字按钮事件
for (int i = 0; i < btn_Number.length; i++) {
final int j=i;
btn_Number[j].setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
txt_Number.setText(txt_Number.getText().toString()+btn_Number[j].getText().toString());
}
});

}
//实现小数点按钮
btn_Dian.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
if (!isDian) {
txt_Number.setText(txt_Number.getText().toString()+btn_Dian.getText().toString());
isDian=true;
}
else {
return;
}
}
});

//实现 符号 按钮事件
for (int i = 0; i < btn_Operator.length; i++) {
final int j=i;
btn_Operator[j].setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
if(txt_Number.getText().equals(""))
{
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("提示") ;
builder.setMessage("请输入数字" ) ;
builder.setPositiveButton("是" , null );
builder.show();
}
else {
if (operator.equals("")) {
number1=txt_Number.getText().toString();
}
else {
number2=txt_Number.getText().toString();
}
operator=btn_Operator[j].getText().toString().trim();
txt_Number.setText("");
isDian=false;
}

}
});

}
//清空按钮的实现
btn_Return.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
if(txt_Number.getText().equals(""))
{
return;
}
else {
txt_Number.setText(txt_Number.getText().toString().substring(0,txt_Number.getText().toString().length()-1));
}
}
});
btn_Clear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
txt_Number.setText("");
number1="";
number2="";
number3="";
operator="";
}
});
//实现 等于号
btn_Deng.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


if(txt_Number.getText().equals(""))
{
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("提示") ;
builder.setMessage("请输入数字" ) ;
builder.setPositiveButton("是" , null );
builder.show();
return;
}
else {
number2=txt_Number.getText().toString();
}



if ("+".equals(operator)) {
number3=String.valueOf(Double.valueOf(number1)+Double.valueOf(number2));
} else if ("-".equals(operator)) {
number3=String.valueOf(Double.valueOf(number1)-Double.valueOf(number2));
}
else if ("*".equals(operator)) {
number3=String.valueOf(Double.valueOf(number1)*Double.valueOf(number2));
}
else if ("÷".equals(operator)) {
if(number2.equals("0"))
{
Toast.makeText(MainActivity.this, "被除数不能为‘0’ !", Toast.LENGTH_LONG).show();
}
else{
number3=String.valueOf(Double.valueOf(number1)/Double.valueOf(number2));
}
}
else {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("提示") ;
builder.setMessage("出现未知错误!" ) ;
builder.setPositiveButton("是" , null );
builder.show();
}
txt_Number.setText(number3);
operator="";
isDian=false;
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
//
// MenuInflater inflater=getMenuInflater();
// inflater.inflate(R.menu.activity_main, menu);
// return true;
// }
//
// public boolean onOptionSItemSelected(MenuItem item){
// if (item.getItemId()==R.id.menu_settings) {
// Intent intent=new Intent(MainActivity.this);
// startService(intent);
// b
// }
// return true;
// }

}


这里还有待完善,介于是给初学者观看,部分功能就没有实现,有兴趣的朋友可以自己实现,在实例化布局文件中的控件是也可以用循环实现,大家可以自己试着实现。下面附上程序源文件,希望能够帮助初学者。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值