當前位置:首頁 » 安卓系統 » android與運算

android與運算

發布時間: 2022-07-30 01:50:45

Ⅰ 關於android軟體開發中如何進行數據計算的問題!

te1對應第一個文本框,以此類推。
int ite1 = Integer.parseInt(te1.getText().toString());
int ite2 = Integer.parseInt(te2.getText().toString());
int ite3 = Integer.parseInt(te3.getText().toString());
int ite4 = Integer.parseInt(te4.getText().toString());
int sum = 0;

if(ite1 < ite4 || ite2 < ite4 || ite3 < ite4) sum = ite1 + ite2 + ite3 + ite4;
te5.setText(String.valueOf(sum));

Ⅱ android實現布局,實現計算器加法運算

用gridlayout很好實現,但是這個值支持4.0以上的版本,網上有一大堆例子,你可以去看看,Linearlayout也可以,不過比較繁雜

Ⅲ android怎麼寫計算器原理

package org.hemingway.calculater;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalculaterMain extends Activity implements View.OnClickListener {
private EditText output;
private EditText input;
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btnadd;
private Button btnsubtract;
private Button btnmultiply;
private Button btndivide;
private Button btnclear;
private Button btnresult;

private String str;//保存數字
private String strold;
private char act;//記錄符號
private int count;//判斷要計算的次數,如果超過一個符號,先算出來一部分
private int result;//計算的結果

private int err;//無錯誤的時候為0
private int flag;//一個標志,如果為1,可以響應運算消息,如果為0,不響應運算消息,只有前面是數字才可以響應運算消息

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

output=(EditText)findViewById(R.id.output);
output.setText(R.string.output);

input=(EditText)findViewById(R.id.input);
input.setText(R.string.input);

btn0=(Button)findViewById(R.id.zero);
btn0.setText(R.string.zero);

btn1=(Button)findViewById(R.id.one);
btn1.setText(R.string.one);

btn2=(Button)findViewById(R.id.two);
btn2.setText(R.string.two);

btn3=(Button)findViewById(R.id.three);
btn3.setText(R.string.three);

btn4=(Button)findViewById(R.id.four);
btn4.setText(R.string.four);

btn5=(Button)findViewById(R.id.five);
btn5.setText(R.string.five);

btn6=(Button)findViewById(R.id.six);
btn6.setText(R.string.six);

btn7=(Button)findViewById(R.id.seven);
btn7.setText(R.string.seven);

btn8=(Button)findViewById(R.id.eight);
btn8.setText(R.string.eight);

btn9=(Button)findViewById(R.id.nine);
btn9.setText(R.string.nine);

btnadd=(Button)findViewById(R.id.add);
btnadd.setText(R.string.add);

btnsubtract=(Button)findViewById(R.id.subtract);
btnsubtract.setText(R.string.subtract);

btnmultiply=(Button)findViewById(R.id.multiply);
btnmultiply.setText(R.string.multiply);

btndivide=(Button)findViewById(R.id.divide);
btndivide.setText(R.string.divide);

btnclear=(Button)findViewById(R.id.clear);
btnclear.setText(R.string.clear);

btnresult=(Button)findViewById(R.id.result);
btnresult.setText(R.string.result);

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);
btnsubtract.setOnClickListener(this);
btnmultiply.setOnClickListener(this);
btndivide.setOnClickListener(this);
btnclear.setOnClickListener(this);
btnresult.setOnClickListener(this);

act=' ';
str="";
strold="";
count=0;
result=0;
err=0;//處理異常,默認無異常
flag=0;//默認不能響應,按下第一個數字後改為1,可以響應

}
public void onClick(View view) {
// TODO Auto-generated method stub

switch(view.getId()){
case R.id.zero: num(0) ;
break;
case R.id.one: num(1) ;
break;
case R.id.two: num(2) ;
break;
case R.id.three: num(3) ;
break;
case R.id.four: num(4) ;
break;
case R.id.five: num(5) ;
break;
case R.id.six: num(6) ;
break;
case R.id.seven: num(7) ;
break;
case R.id.eight: num(8) ;
break;
case R.id.nine: num(9) ;
break;

case R.id.add: add() ;
break;
case R.id.subtract: sub() ;
break;
case R.id.multiply: multiply() ;
break;
case R.id.divide: divide() ;
break;
case R.id.clear: clear();
break;
case R.id.result:
result() ;
if(err!=1&&flag!=0)
output.setText(String.valueOf(result));
break;
default:
break;
}
input.setText(strold+act+str);
}
public void num(int n){
str=str+String.valueOf(n);
flag=1;
}
public void add(){
if(flag!=0)
{
check();
act='+';
flag=0;
}
}
public void sub(){
if(flag!=0)
{
check();
act='-';
flag=0;
}
}
public void multiply(){
if(flag!=0)
{
check();
act='*';
flag=0;
}
}
public void divide(){
if(flag!=0)
{
check();
act='/';
flag=0;
}
}
public void clear(){
str=strold="";
count=0;
act=' ';
result=0;
flag=0;
input.setText(strold+act+str);
output.setText("");
}
public void check(){
if(count>=1)
{
result();
str=String.valueOf(result);

}
strold=str;
str="";
count++;

}
public void result(){
int a,b;
a=Integer.parseInt(strold);
b=Integer.parseInt(str);
if(flag!=0)
{
if(b==0&&act=='/') {
clear();
output.setText("除數不能為零!");
err=1;
}
result=0;
if(err!=1){
switch(act){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
default:
break;
}
}
}
}
public void onConfigurationChanged(Configuration newConfig){
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//橫向
setContentView(R.layout.main);
}else{
//豎向
setContentView(R.layout.main);
}

}

}

R.java

/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package org.hemingway.calculater;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final int icon=0x7f020001;
}
public static final class id {
public static final int add=0x7f050005;
public static final int clear=0x7f05000f;
public static final int divide=0x7f050011;
public static final int eight=0x7f050003;
public static final int five=0x7f050007;
public static final int four=0x7f050006;
public static final int input=0x7f050000;
public static final int multiply=0x7f05000d;
public static final int nine=0x7f050004;
public static final int one=0x7f05000a;
public static final int output=0x7f050001;
public static final int result=0x7f050010;
public static final int seven=0x7f050002;
public static final int six=0x7f050008;
public static final int subtract=0x7f050009;
public static final int three=0x7f05000c;
public static final int two=0x7f05000b;
public static final int zero=0x7f05000e;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int add=0x7f04000e;
public static final int app_name=0x7f040001;
public static final int clear=0x7f040012;
public static final int divide=0x7f040011;
public static final int eight=0x7f04000c;
public static final int five=0x7f040009;
public static final int four=0x7f040008;
public static final int hello=0x7f040000;
public static final int input=0x7f040002;
public static final int multiply=0x7f040010;
public static final int nine=0x7f04000d;
public static final int one=0x7f040005;
public static final int output=0x7f040003;
public static final int result=0x7f040013;
public static final int seven=0x7f04000b;
public static final int six=0x7f04000a;
public static final int subtract=0x7f04000f;
public static final int three=0x7f040007;
public static final int two=0x7f040006;
public static final int zero=0x7f040004;
}
}

熱點內容
用近似歸演算法 發布:2025-01-21 00:51:56 瀏覽:517
php顯示資料庫中圖片 發布:2025-01-21 00:44:34 瀏覽:145
如何在伺服器中找文件 發布:2025-01-21 00:38:50 瀏覽:910
Cmdpython命令 發布:2025-01-21 00:30:38 瀏覽:758
mac常用解壓 發布:2025-01-21 00:01:47 瀏覽:691
linuxcpu使用 發布:2025-01-21 00:00:59 瀏覽:849
成套供應配電櫃有哪些配置 發布:2025-01-21 00:00:52 瀏覽:120
GO編譯器PDF 發布:2025-01-21 00:00:52 瀏覽:704
osu上傳成績 發布:2025-01-20 23:59:57 瀏覽:641
了解sql 發布:2025-01-20 23:58:39 瀏覽:656