當前位置:首頁 » 安卓系統 » 計算器android

計算器android

發布時間: 2023-09-08 10:24:26

A. android計算器,實現連續使用加號來操作數。

private void getResult() {

needclear = true;

String exp = et_showview.getText().toString();

double r = 0;

int space = exp.indexOf(' ');//用於搜索空格位置

String s1 = exp.substring(0, space);//s1用於保存第一個運算數

String op = exp.substring(space + 1, space + 2);//op用於保存運算符

String s2 = exp.substring(space + 3);//s2用於保存第二個運算數

double arg1 = Double.parseDouble(s1);//將運算數從string轉換為Single

double arg2 = Double.parseDouble(s2);

if(op.equals("+")){

r = arg1 + arg2;

}else if(op.equals("-")){

r = arg1 - arg2;

}else if(op.equals("×")){

r = arg1 * arg2;

}else if(op.equals("÷")){

if (arg2 == 0)

{

r=0;

}

else

{

r = arg1 / arg2;

}

}

if(!s1.contains(".")&&!s2.contains(".")){

int result = (int)r;

et_showview.setText(result+"");

}else{

et_showview.setText(r+"");

}

}

//

// /***

// * @param exp

// * 算數表達式

// * @return 根據表達式返回結果

// */

// private String getRs(String exp) {

// Interpreter bsh = new Interpreter();

// Number result = null;

// try {

// exp = filterExp(exp);

// result = (Number) bsh.eval(exp);

// } catch (EvalError e) {

// e.printStackTrace();

// return "0";

// }

// return result.doubleValue() + "";

// }

//

// /**

// * @param exp

// * 算數表達式

// * @return 因為計算過程中,全程需要有小數參與.

// */

// private String filterExp(String exp) {

// String num[] = exp.split("");

// String temp = null;

// double dtemp = 0 ;

// String str_temp="";

// for (int i = 0; i < num.length; i++) {

// temp = num[i];

//

// if (temp.equals("+") || temp.equals("-") || temp.equals("×")

// || temp.equals("÷")) {

// if(dtemp==0){

// dtemp=Double.parseDouble(str_temp);

// }

//

// str_temp="";

// }else{

// str_temp=str_temp+temp;

// }

// }

//

// int begin = 0, end = 0;

// for (int i = 1; i < num.length; i++) {

// temp = num[i];

// if (temp.matches("[+-/()*]")) {

// if (temp.equals("."))

// continue;

// end = i - 1;

// temp = exp.substring(begin, end);

// if (temp.trim().length() > 0 && temp.indexOf(".") < 0)

// num[i - 1] = num[i - 1] + ".0";

// begin = end + 1;

// }

// }

// return Arrays.toString(num).replaceAll("[\[\], ]", "");

java">

B. 開發一個簡易的計算器APP程序 Android源代碼

下面是效果展示:

復制代碼代碼如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="s/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:text="@string/tvResult"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:layout_marginLeft="10dp"
android:text="@string/btnbackspace"/>
<Button
android:id="@+id/btnCE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:text="@string/btnCE"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn7"/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn8"/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn9"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnDiv"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn4"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn5"/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn6"/>
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnMul"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn3"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnAdd"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn0"/>
<Button
android:id="@+id/btnC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnC"/>
<Button
android:id="@+id/btnEqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnEqu"/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnSub"/>
</LinearLayout>
</LinearLayout>

復制代碼代碼如下:


package com.example.week2;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener{

//聲明一些控制項
Button btn0=null;
Button btn1=null;
Button btn2=null;
Button btn3=null;
Button btn4=null;
Button btn5=null;
Button btn6=null;
Button btn7=null;
Button btn8=null;
Button btn9=null;
Button btnBackspace=null;
Button btnCE=null;
Button btnC=null;
Button btnAdd=null;
Button btnSub=null;
Button btnMul=null;
Button btnDiv=null;
Button btnEqu=null;
TextView tvResult=null;
//聲明兩個參數。接收tvResult前後的值
double num1=0,num2=0;
double Result=0;//計算結果
int op=0;//判斷操作數,
boolean isClickEqu=false;//判斷是否按了「=」按鈕

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//從布局文件中獲取控制項,
btn0=(Button)findViewById(R.id.btn0);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btnBackspace=(Button)findViewById(R.id.btnBackspace);
btnCE=(Button)findViewById(R.id.btnCE);
btnC=(Button)findViewById(R.id.btnC);
btnEqu=(Button)findViewById(R.id.btnEqu);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnSub=(Button)findViewById(R.id.btnSub);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
tvResult=(TextView)findViewById(R.id.tvResult);

//添加監聽
btnBackspace.setOnClickListener(this);
btnCE.setOnClickListener(this);

btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);


btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnEqu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//btnBackspace和CE--------------------
case R.id.btnBackspace:
String myStr=tvResult.getText().toString();
try {
tvResult.setText(myStr.substring(0, myStr.length()-1));
} catch (Exception e) {
tvResult.setText("");
}

break;
case R.id.btnCE:
tvResult.setText(null);
break;

//btn0--9---------------------------
case R.id.btn0:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString=tvResult.getText().toString();
myString+="0";
tvResult.setText(myString);
break;
case R.id.btn1:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString1=tvResult.getText().toString();
myString1+="1";
tvResult.setText(myString1);
break;
case R.id.btn2:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString2=tvResult.getText().toString();
myString2+="2";
tvResult.setText(myString2);
break;
case R.id.btn3:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString3=tvResult.getText().toString();
myString3+="3";
tvResult.setText(myString3);
break;
cas

熱點內容
青驕如何重置賬號密碼 發布:2025-02-01 09:57:51 瀏覽:520
阿里雲伺服器鏡像市場 發布:2025-02-01 09:46:04 瀏覽:525
任子行伺服器管理口默認地址 發布:2025-02-01 09:42:58 瀏覽:996
設備作為FTP客戶端時 發布:2025-02-01 09:35:07 瀏覽:935
安卓如何登錄ios明日之後 發布:2025-02-01 09:31:59 瀏覽:306
怎麼查看手機存儲卡 發布:2025-02-01 09:31:51 瀏覽:341
java知識點總結 發布:2025-02-01 09:08:32 瀏覽:685
如何在手機版給伺服器加光影 發布:2025-02-01 09:02:14 瀏覽:728
簡單神器安卓系統的哪個好 發布:2025-02-01 09:00:48 瀏覽:355
社保卡密碼如何異地改密碼 發布:2025-02-01 08:57:22 瀏覽:33