最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?Studio實(shí)現(xiàn)簡易計(jì)算器App?(Java語言版)

 更新時間:2022年05月20日 16:53:31   作者:lijin8080  
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡易計(jì)算器App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡易計(jì)算器App的具體代碼,供大家參考,具體內(nèi)容如下

效果演示

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical" android:layout_width="match_parent"
? ? android:layout_height="match_parent">

? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="2"
? ? ? ? android:orientation="vertical">
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/mini"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:textSize="40dp"
? ? ? ? ? ? android:text=""
? ? ? ? ? ? android:textDirection="rtl"/>
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/max"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:textSize="100dp"
? ? ? ? ? ? android:text=""
? ? ? ? ? ? android:textDirection="rtl"/>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="%"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="surplus"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="CE"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="clearce"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="C"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="clearc"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="?"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="backsprce"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="1/x"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="reciprocal"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="x2"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="square"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="2√x"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="squareroot"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="÷"
? ? ? ? ? ? ? ? android:textSize="60dp"
? ? ? ? ? ? ? ? android:onClick="division"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="7"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_7"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="8"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_8"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="9"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_9"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="×"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="multiplication"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="4"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_4"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="5"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_5"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="6"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_6"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="-"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="subtraction"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="1"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_1"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="2"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_2"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="3"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_3"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="+"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="addition"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:orientation="horizontal">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="±"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="change"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="0"
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="number_0"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="."
? ? ? ? ? ? ? ? android:textSize="30dp"/>
? ? ? ? </LinearLayout>
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_weight="1">
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:text="="
? ? ? ? ? ? ? ? android:textSize="30dp"
? ? ? ? ? ? ? ? android:onClick="equal"/>
? ? ? ? </LinearLayout>

? ? </LinearLayout>

</LinearLayout>

Java文件

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

import androidx.annotation.Nullable;

public class MainActivity extends Activity {

? ? private TextView tv1;
? ? private TextView tv2;
? ? private StringBuilder viewStr;
? ? private int index = 0;

? ? @Override
? ? protected void onCreate(@Nullable Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main_calc);
? ? ? ? tv1 = (TextView) findViewById(R.id.mini);
? ? ? ? tv2 = (TextView) findViewById(R.id.max);
? ? ? ? viewStr = new StringBuilder();
? ? ? ? index = 0;
? ? }

? ? //加減乘除、求余簡單運(yùn)算方法
? ? public String calc(StringBuilder strB) {
? ? ? ? String allS = strB.toString();
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? int x = 0; int y = 0;
? ? ? ? String result = null;
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = strB.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0) {
? ? ? ? ? ? ? ? String a = allS.split(asmd[i])[0];
? ? ? ? ? ? ? ? String b = allS.split(asmd[i])[1];
? ? ? ? ? ? ? ? if (i == 0) {result = String.valueOf(Integer.parseInt(a) + Integer.parseInt(b));}
? ? ? ? ? ? ? ? if (i == 1) {result = String.valueOf(Integer.parseInt(a) - Integer.parseInt(b));}
? ? ? ? ? ? ? ? if (i == 2) {result = String.valueOf(Integer.parseInt(a) * Integer.parseInt(b));}
? ? ? ? ? ? ? ? if (i == 3) {
? ? ? ? ? ? ? ? ? ? if (Integer.parseInt(b) == 0) {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(this,"0不能為除數(shù)",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ? result = String.valueOf(Integer.parseInt(a));
? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? result = String.valueOf(Integer.parseInt(a) / Integer.parseInt(b));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (i == 4) {result = String.valueOf(Integer.parseInt(a) % Integer.parseInt(b));}
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return result;
? ? }

? ? //數(shù)字按鈕事件
? ? public void number_0(View view) { viewStr.append("0"); index ++ ; tv2.setText(viewStr); }
? ? public void number_1(View view) { viewStr.append("1"); index ++ ; tv2.setText(viewStr); }
? ? public void number_2(View view) { viewStr.append("2"); index ++ ; tv2.setText(viewStr); }
? ? public void number_3(View view) { viewStr.append("3"); index ++ ; tv2.setText(viewStr); }
? ? public void number_4(View view) { viewStr.append("4"); index ++ ; tv2.setText(viewStr); }
? ? public void number_5(View view) { viewStr.append("5"); index ++ ; tv2.setText(viewStr); }
? ? public void number_6(View view) { viewStr.append("6"); index ++ ; tv2.setText(viewStr); }
? ? public void number_7(View view) { viewStr.append("7"); index ++ ; tv2.setText(viewStr); }
? ? public void number_8(View view) { viewStr.append("8"); index ++ ; tv2.setText(viewStr); }
? ? public void number_9(View view) { viewStr.append("9"); index ++ ; tv2.setText(viewStr); }

? ? //回退按鈕事件
? ? public void backsprce(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? index = viewStr.length();
? ? ? ? viewStr.deleteCharAt(--index); tv2.setText(viewStr);
? ? }

? ? //符號改變按鈕事件
? ? public void change(View view) {
? ? ? ? String allS = viewStr.toString();
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? int inde = 0;
? ? ? ? String a = null; ? ?//保存字符串中運(yùn)算符部分
? ? ? ? String b = null; ? ?//保存字符串中數(shù)字部分
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde != -1) {
? ? ? ? ? ? ? ? a = asmd[i];
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //A 字符形 改變 A 正負(fù)值
? ? ? ? if (inde == -1) {
? ? ? ? ? ? clearc(view);
? ? ? ? ? ? int c = - Integer.parseInt(allS);
? ? ? ? ? ? viewStr.append(String.valueOf(c));
? ? ? ? ? ? index = String.valueOf(c).length();
? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? }
? ? ? ? //A + 字符串形 改變 A 正負(fù)值 暫時無法實(shí)現(xiàn),待后續(xù)優(yōu)化
? ? ? ? if (inde == index - 1) {
? ? ? ? ? ? return;
// ? ? ? ? ? ?clearc(view);
// ? ? ? ? ? ?int c = - Integer.valueOf(allS.split(a)[0]);
// ? ? ? ? ? ?viewStr.append(String.valueOf(c));
// ? ? ? ? ? ?index = String.valueOf(c).length();
// ? ? ? ? ? ?tv2.setText(viewStr);
? ? ? ? }
? ? ? ? //A + B 字符串形 改變 B 正負(fù)值
? ? ? ? if (inde >= 0 && inde < index) {
? ? ? ? ? ? clearc(view);
? ? ? ? ? ? b = allS.split(a)[0];
? ? ? ? ? ? int c = - Integer.parseInt(allS.split(a)[1]);
? ? ? ? ? ? viewStr.append(b).append(a).append(String.valueOf(c));
? ? ? ? ? ? index = String.valueOf(c).length();
? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? }
? ? }

? ? //清空按鈕事件
? ? public void clearc(View view) {
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? viewStr = temp.append("");
? ? ? ? tv2.setText(viewStr);
? ? ? ? index = 0;
? ? }
? ? public void clearce(View view) {
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? viewStr = temp.append("");
? ? ? ? tv1.setText("");
? ? ? ? tv2.setText(viewStr);
? ? ? ? index = 0;
? ? }

? ? //等于按鈕事件
? ? public void equal(View view) {
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && inde != index-1) {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a);
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }else if (inde > 0 && inde == index-1) {
? ? ? ? ? ? ? ? viewStr.deleteCharAt(--index);
? ? ? ? ? ? ? ? tv1.setText(viewStr);
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? tv1.setText(viewStr);
? ? ? ? tv2.setText(viewStr);
? ? }

? ? //加減乘除按鈕事件
? ? public void addition(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a).append('+');
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? char a = viewStr.charAt(index-1);
? ? ? ? if (a == '+') { return; }
? ? ? ? if (a == '-' || a == '×' || a == '÷' || a == '%') {
? ? ? ? ? ? viewStr.setCharAt(index-1,'+');
? ? ? ? }else {
? ? ? ? ? ? viewStr.append("+");
? ? ? ? ? ? index ++ ;
? ? ? ? }
? ? ? ? tv2.setText(viewStr);
? ? }
? ? public void subtraction(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a).append('-');
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? char a = viewStr.charAt(index-1);
? ? ? ? if (a == '-') { return; }
? ? ? ? if (a == '+' || a == '×' || a == '÷' || a == '%') {
? ? ? ? ? ? viewStr.setCharAt(index-1,'-');
? ? ? ? }else {
? ? ? ? ? ? viewStr.append("-");
? ? ? ? ? ? index ++ ;
? ? ? ? }
? ? ? ? tv2.setText(viewStr);
? ? }
? ? public void multiplication(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a).append('×');
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? char a = viewStr.charAt(index-1);
? ? ? ? if (a == '×') { return; }
? ? ? ? if (a == '+' || a == '-' || a == '÷' || a == '%') {
? ? ? ? ? ? viewStr.setCharAt(index-1,'×');
? ? ? ? }else {
? ? ? ? ? ? viewStr.append("×");
? ? ? ? ? ? index ++ ;
? ? ? ? }
? ? ? ? tv2.setText(viewStr);
? ? }
? ? public void division(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a).append('÷');
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? char a = viewStr.charAt(index-1);
? ? ? ? if (a == '÷') { return; }
? ? ? ? if (a == '+' || a == '-' || a == '×' || a == '%') {
? ? ? ? ? ? viewStr.setCharAt(index-1,'÷');
? ? ? ? }else {
? ? ? ? ? ? viewStr.append("÷");
? ? ? ? ? ? index ++ ;
? ? ? ? }
? ? ? ? tv2.setText(viewStr);
? ? }

? ? //求余按鈕事件
? ? public void surplus(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? StringBuilder temp = new StringBuilder();
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') {
? ? ? ? ? ? ? ? tv1.setText(calc(viewStr));
? ? ? ? ? ? ? ? String a = calc(viewStr);
? ? ? ? ? ? ? ? viewStr = temp.append(a).append('%');
? ? ? ? ? ? ? ? tv2.setText(viewStr);
? ? ? ? ? ? ? ? index = viewStr.length();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? char a = viewStr.charAt(index-1);
? ? ? ? if (a == '%') { return; }
? ? ? ? if (a == '+' || a == '-' || a == '×' || a == '÷') {
? ? ? ? ? ? viewStr.setCharAt(index-1,'%');
? ? ? ? }else {
? ? ? ? ? ? viewStr.append("%");
? ? ? ? ? ? index ++ ;
? ? ? ? }
? ? ? ? tv2.setText(viewStr);
? ? }

? ? //求倒數(shù)按鈕事件
? ? public void reciprocal(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > -1) { return; }
? ? ? ? }
? ? ? ? int a = Integer.parseInt(viewStr.toString().trim());
? ? ? ? if (a == 0) {
? ? ? ? ? ? Toast.makeText(this,"0不能為除數(shù)",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? clearc(view);
? ? ? ? viewStr.append(1/a);
? ? ? ? index = String.valueOf(1/a).length();
? ? ? ? tv2.setText(viewStr);
? ? }

? ? //平方按鈕事件
? ? public void square(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > -1) { return; }
? ? ? ? }
? ? ? ? int a = Integer.parseInt(viewStr.toString().trim());
? ? ? ? clearc(view);
? ? ? ? viewStr.append(a*a);
? ? ? ? index = String.valueOf(a*a).length();
? ? ? ? tv2.setText(viewStr);
? ? }

? ? //開平方按鈕事件
? ? public void squareroot(View view) {
? ? ? ? if (viewStr.length() == 0) return;
? ? ? ? String[] asmd = {"+","-","×","÷","%"};
? ? ? ? for (int i = 0; i < 5; i ++) {
? ? ? ? ? ? int inde = viewStr.indexOf(asmd[i]);
? ? ? ? ? ? if (inde > -1) { return; }
? ? ? ? }
? ? ? ? int a = Integer.parseInt(viewStr.toString().trim());
? ? ? ? clearc(view);
? ? ? ? viewStr.append((int)Math.sqrt(a));
? ? ? ? index = String.valueOf((int)Math.sqrt(a)).length();
? ? ? ? tv2.setText(viewStr);
? ? }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Kotlin空安全空類型淺談

    Kotlin空安全空類型淺談

    這篇文章主要為大家介紹了Kotlin空安全空類型的實(shí)用技巧淺談,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android獲取聯(lián)系人頭像的方法

    Android獲取聯(lián)系人頭像的方法

    這篇文章主要介紹了Android獲取聯(lián)系人頭像的方法,結(jié)合實(shí)例形式分析了Android調(diào)用聯(lián)系人頭像的相關(guān)操作技巧,涉及Bitmap對象及ImageView的使用方法,需要的朋友可以參考下
    2016-08-08
  • Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】

    Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能,涉及Android Switch開關(guān)控件樣式設(shè)置與事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • Android 中 Tweened animation的實(shí)例詳解

    Android 中 Tweened animation的實(shí)例詳解

    這篇文章主要介紹了Android 中 Tweened animation的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • android球形水波百分比控件代碼

    android球形水波百分比控件代碼

    本篇文章主要是介紹android球形水波百分比控件,現(xiàn)在很多地方都能用的,有需要的可以來了解一下。
    2016-11-11
  • Android自定義View圓形和拖動圓跟隨手指拖動

    Android自定義View圓形和拖動圓跟隨手指拖動

    這篇文章主要介紹了Android自定義View圓形和拖動圓跟隨手指拖動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android中Service(后臺服務(wù))詳解

    Android中Service(后臺服務(wù))詳解

    這篇文章主要介紹了Android中Service(后臺服務(wù))詳解,本文講解了Service的概念、作用、生命周期、啟動方式和代碼實(shí)例等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • Android中EditText setText方法的踩坑實(shí)戰(zhàn)

    Android中EditText setText方法的踩坑實(shí)戰(zhàn)

    這篇文章主要給大家分享了一些關(guān)于Android中EditText setText方法的踩坑記錄,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android中Bitmap、File與Uri之間的簡單記錄

    Android中Bitmap、File與Uri之間的簡單記錄

    這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Kotlin?ContentProvider使用方法介紹

    Kotlin?ContentProvider使用方法介紹

    ContentProvider內(nèi)容提供者,主要用于再不同的應(yīng)用程序之前實(shí)現(xiàn)數(shù)據(jù)共享的功能,它提供了一套完整的機(jī)制,允許一個程序訪問另外一個程序的數(shù)據(jù),同時還能保證數(shù)據(jù)的安全性
    2022-09-09

最新評論

大竹县| 井冈山市| 澄江县| 五华县| 乡城县| 什邡市| 乌兰浩特市| 乌拉特中旗| 嘉定区| 临夏县| 龙里县| 福建省| 南汇区| 项城市| 东宁县| 横峰县| 屏山县| 崇礼县| 泰宁县| 兴安县| 吉林省| 罗山县| 云安县| 绵竹市| 亚东县| 彭阳县| 建湖县| 瑞金市| 平江县| 鲜城| 巴青县| 巴南区| 宣威市| 堆龙德庆县| 车险| 关岭| 吉林市| 高密市| 醴陵市| 六枝特区| 武功县|