首页 > 移动平台 > 详细

Android 简单计算器<可以看看>

时间:2015-09-23 02:14:44      阅读:246      评论:0      收藏:0      [点我收藏+]

Android 简单计算器

?

本例子基于Android googl sdk一书的计算器实现例子:
1 创建三个TextView对象,两个 EditText对象,四个Button对象
main.xml为
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
? android:id="@+id/widget0"
? android:layout_width="fill_parent"
? android:layout_height="fill_parent"
? xmlns:android="http://itlanbao.com/preview.aspx#1,0"
? >
? <EditText android:id="@+id/EditText1"
?????? android:layout_height="wrap_content"
?????? android:layout_width="47px"
?????? android:textSize="18sp"
?????? android:layout_x="26px"
?????? android:layout_y="52px"
?????? />
?? <EditText android:id="@+id/EditText2"
?????? android:layout_height="wrap_content"
?????? android:layout_width="45px"
?????? android:textSize="18sp"
?????? android:layout_x="128px"
?????? android:layout_y="51px"/>
? <TextView
??? android:id="@+id/MyTextView1"
??? android:layout_width="30px"
??? android:layout_height="33px"
??? android:textSize="18sp"
??? android:layout_x="92px"
??? android:layout_y="60px"
?? />
??? <TextView
??? android:id="@+id/MyTextView3"
??? android:layout_width="18px"
??? android:layout_height="wrap_content"
??? android:text="="
??? android:textSize="18sp"
??? android:layout_x="189px"
??? android:layout_y="63px"
??? />
???? <TextView
??? android:id="@+id/MyTextView2"
??? android:layout_width="30px"
??? android:layout_height="wrap_content"
??? android:textSize="18sp"
??? android:layout_x="219px"
??? android:layout_y="61px"
??? />
? <Button
??? android:id="@+id/button1"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:textSize="25sp"
??? android:layout_x="12px"
??? android:layout_y="161px"
??? android:text="@string/but_str1" />
??? <Button
??? android:id="@+id/button2"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:textSize="25sp"
??? android:layout_x="85px"
??? android:layout_y="159px"
??? android:text="@string/but_str2" />
??? <Button
??? android:id="@+id/button3"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:textSize="25sp"
??? android:layout_x="158px"
??? android:layout_y="161px"
??? android:text="@string/but_str3" />
??? <Button
??? android:id="@+id/button4"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:textSize="25sp"
??? android:layout_x="239px"
??? android:layout_y="161px"
??? android:text="@string/but_str4" />
</AbsoluteLayout>
这里样式还有一点问题
按钮显示的内容通过String.xml 获取。也可直接在main.xml中修改android:text="+"指定如上指定显示“+”.
Activity 对象
package com.app.android;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyFirstActivity extends Activity {
??? /** Called when the activity is first created. */
private Button button1;
private TextView textView1;
private TextView textView2;
private Button button2;
private Button button3;
private Button button4;
private EditText editText1;
private EditText editText2;
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? button1=(Button)findViewById(R.id.button1);
??????? button2=(Button)findViewById(R.id.button2);
??????? button3=(Button)findViewById(R.id.button3);
??????? button4=(Button)findViewById(R.id.button4);
??????? editText1=(EditText)findViewById(R.id.EditText1);
??????? editText2=(EditText)findViewById(R.id.EditText2);
??????? textView1=(TextView)findViewById(R.id.MyTextView1);
??????? textView2=(TextView)findViewById(R.id.MyTextView2);
??????? button1.setOnClickListener(new Button.OnClickListener()
??????? {
??????????? public void onClick(View v)
??????????? {
????????????? // TODO Auto-generated method stub
??????????? if(confirm(editText1,editText2)){
??????????? textView1.setText("+");
??????????? String strRet=format(Float.parseFloat(editText1.getText().toString())+Float.parseFloat(editText2.getText().toString()));
???????????? textView2.setText(strRet);
??????????? }
??????????? }
????????? });
??????? button2.setOnClickListener(new Button.OnClickListener()
??????? {
??????????? public void onClick(View v)
??????????? {
????????????? // TODO Auto-generated method stub
??????????? if(confirm(editText1,editText2)){
??????????? textView1.setText("-");
??????????? String strRet=format(Float.parseFloat(editText1.getText().toString())-Float.parseFloat(editText2.getText().toString()));
???????????? textView2.setText(strRet);
??????????? }
??????????? }
????????? });
??????? button3.setOnClickListener(new Button.OnClickListener()
??????? {
??????????? public void onClick(View v)
??????????? {
????????????? // TODO Auto-generated method stub
??????????? if(confirm(editText1,editText2)){
??????????? textView1.setText("*");
??????????? String strRet=format(Float.parseFloat(editText1.getText().toString())*Float.parseFloat(editText2.getText().toString()));
???????????? textView2.setText(strRet);
??????????? }}
????????? });
??????? button4.setOnClickListener(new Button.OnClickListener()
??????? {
??????????? public void onClick(View v)
??????????? {
????????????? // TODO Auto-generated method stub
??????????? if(confirm(editText1,editText2)){;
??????????? textView1.setText("/");
??????????? String strRet=format(Float.parseFloat(editText1.getText().toString())/Float.parseFloat(editText2.getText().toString()));
??????????? textView2.setText(strRet);
??????????? }}
????????? });
?????
??? }
??? /**
???? * 格式化结果
???? * @param num
???? * @return
???? */
??? private String format(float num){
??? NumberFormat formater=new DecimalFormat("0.00");
??? String s=formater.format(num);
??????? return s;
??? }
??? /**
???? * 确认运算项是否完整
???? * @param text1
???? * @param text2
???? * @return
???? */
??? private boolean confirm(EditText text1,EditText text2){
??? String editText1=text1.getText().toString();
??? String editText2=text2.getText().toString();
??? //System.out.println(editText1+","+editText2);
??? if((editText1!=null&&editText1.equals(""))||(editText2!=null&&editText2.equals(""))){
??? new AlertDialog.Builder(MyFirstActivity.this).setTitle(R.string.title).setMessage(R.string.msg).setPositiveButton(
??? R.string.ok,new DialogInterface.OnClickListener(){
??? public void onClick(DialogInterface dialoginterface,int i){
??? dialoginterface.dismiss();
??? }
??? }?
??? ).show();
??? return false;
??? }
??? else{return true;}
??? }
}
方法都已经注释了

?

更多例子下载:http://itlanbao.com/preview.aspx#1,0

Android 简单计算器<可以看看>

原文:http://3105165143.iteye.com/blog/2245175

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!