首页 / ANDROID / Android 简单计算器
Android 简单计算器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android 简单计算器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6869字,纯文字阅读大概需要10分钟。
内容图文
![Android 简单计算器](/upload/InfoBanner/zyjiaocheng/1277/a7d2e5421d7a4df1acc69c90f0a22a3e.jpg)
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
原文:http://3083127593.iteye.com/blog/2245321
内容总结
以上是互联网集市为您收集整理的Android 简单计算器全部内容,希望文章能够帮你解决Android 简单计算器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。