AIbridge로 간단한 계산기 만들기



먼저 실행 동영상



java 파일
package com.example.acer.aibridgeapp;
// you need the following imports in all Java Bridge apps


import com.google.appinventor.components.runtime.Button;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.HandlesEventDispatching;
import com.google.appinventor.components.runtime.HorizontalArrangement;
import com.google.appinventor.components.runtime.Label;
import com.google.appinventor.components.runtime.TextBox;

import java.util.regex.*;

public class Screen1 extends Form implements HandlesEventDispatching
{

   // declare all your components as instance variables    private Label Totallable;
    private HorizontalArrangement ha;
   private HorizontalArrangement ha2;
   private HorizontalArrangement ha3;
   private HorizontalArrangement ha4;
   private HorizontalArrangement ha5;
   private HorizontalArrangement ha6;
   private Button Button1;
   private Button Button2;
   private Button Button3;
   private Button Button4;
   private Button Button5;
   private Button Button6;
   private Button Button7;
   private Button Button8;
   private Button Button9;
   private Button Button0;
   private Button Buttonminus;
   private Button Buttonplus;
   private Button Buttondivide;
   private Button Buttonmulti;
   private Button Buttonclear;
   private TextBox tb;
   private TextBox tb2;
   private Button Cal;
    String txt = ""; String txt2 ="";
    String [] arr;int result=0; Double result2;
   Pattern p = Pattern.compile("\\D+");
    public static boolean isInt(String s) {
        try {
            Integer.parseInt(s);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
protected void $define()
   {  
      // create the button component        this.ScreenOrientation("portrait");
        this.Title("Calculation");
        this.BackgroundColor(COLOR_WHITE);
        this.Scrollable(true);
        Totallable = new Label(this);
        Totallable.Text("Touch the Button");
        Totallable.FontSize(20.0f);
        Totallable.Width(LENGTH_FILL_PARENT);
        Totallable.TextAlignment(ALIGNMENT_CENTER);
        Totallable.FontBold(true);
        ha = new HorizontalArrangement(this);
      ha.Width(LENGTH_FILL_PARENT);
      Button1 = new Button(ha);Button1.Text( "1" );  Button1.BackgroundColor(COLOR_GRAY );
      Button1.Width(480);Button1.Height(300);Button1.FontSize(25.0f);Button1.FontBold(true);
      Button2 = new Button(ha);Button2.Text( "2" );  Button2.BackgroundColor(COLOR_GRAY );
      Button2.Width(480);Button2.Height(300);Button2.FontSize(25.0f);Button2.FontBold(true);
      Button3 = new Button(ha);Button3.Text( "3" );  Button3.BackgroundColor(COLOR_GRAY );
      Button3.Width(480);Button3.Height(300);Button3.FontSize(25.0f);Button3.FontBold(true);
      ha2 = new HorizontalArrangement(this);
      Button4 = new Button(ha2); Button4.Text( "4" );Button4.BackgroundColor(COLOR_GRAY );
      Button4.Width(480);Button4.Height(300);Button4.FontSize(25.0f);Button4.FontBold(true);
      Button5 = new Button(ha2); Button5.Text( "5" );Button5.BackgroundColor(COLOR_GRAY );
      Button5.Width(480);Button5.Height(300);Button5.FontSize(25.0f);Button5.FontBold(true);
      Button6 = new Button(ha2); Button6.Text( "6" );Button6.BackgroundColor(COLOR_GRAY );
      Button6.Width(480);Button6.Height(300);Button6.FontSize(25.0f);Button6.FontBold(true);
      ha3 = new HorizontalArrangement(this);
      Button7 = new Button(ha3); Button7.Text( "7" );Button7.BackgroundColor(COLOR_GRAY );
      Button7.Width(480);Button7.Height(300);Button7.FontSize(25.0f);Button7.FontBold(true);
      Button8 = new Button(ha3); Button8.Text( "8" );Button8.BackgroundColor(COLOR_GRAY );
      Button8.Width(480);Button8.Height(300);Button8.FontSize(25.0f);Button8.FontBold(true);
      Button9 = new Button(ha3); Button9.Text( "9" );Button9.BackgroundColor(COLOR_GRAY );
      Button9.Width(480);Button9.Height(300);Button9.FontSize(25.0f);Button9.FontBold(true);
      ha4 = new HorizontalArrangement(this);
      Button0 = new Button(ha4); Button0.Text( "0" );Button0.BackgroundColor(COLOR_GRAY );
      Button0.Width(480);Button0.Height(300);Button0.FontSize(25.0f);Button0.FontBold(true);
      Buttonplus = new Button(ha4);Buttonplus.Text( "+" );   Buttonplus.BackgroundColor(COLOR_GRAY );
      Buttonplus.Width(480);Buttonplus.Height(300);Buttonplus.FontSize(25.0f);Buttonplus.FontBold(true);
      Buttonminus = new Button(ha4);Buttonminus.Text( "-" );Buttonminus.BackgroundColor(COLOR_GRAY );
      Buttonminus.Width(480);Buttonminus.Height(300);Buttonminus.FontSize(25.0f);Buttonminus.FontBold(true);
      ha5 = new HorizontalArrangement(this);
      Buttondivide = new Button(ha5);Buttondivide.Text( "/" );Buttondivide.BackgroundColor(COLOR_GRAY );
      Buttondivide.Width(480);Buttondivide.Height(300);Buttondivide.FontSize(25.0f);Buttondivide.FontBold(true);
      Buttonmulti = new Button(ha5);Buttonmulti.Text( "*" );Buttonmulti.BackgroundColor(COLOR_GRAY );
      Buttonmulti.Width(480);Buttonmulti.Height(300);Buttonmulti.FontSize(25.0f);Buttonmulti.FontBold(true);
      Buttonclear = new Button(ha5);Buttonclear.Text("C");Buttonclear.BackgroundColor(COLOR_GRAY);
      Buttonclear.Width(480);Buttonclear.Height(300);Buttonclear.FontSize(25.0f);Buttonclear.FontBold(true);
      tb = new TextBox(this);tb.BackgroundColor(COLOR_LTGRAY);tb.Width(LENGTH_FILL_PARENT);tb.FontSize(25.0f);tb.FontBold(true);
      ha6 = new HorizontalArrangement(this);
      ha6.Visible(true);ha6.Height(50);
      Cal = new Button(this);Cal.Text( "=" );Cal.BackgroundColor(COLOR_GRAY );Cal.Width(480);Cal.Height(300);Cal.FontSize(25.0f);Cal.FontBold(true);
      tb2 = new TextBox(this);tb2.BackgroundColor(COLOR_LTGRAY);tb2.Width(LENGTH_FILL_PARENT);tb2.FontBold(true);tb2.FontSize(25.0f);
      EventDispatcher.registerEventForDelegation( this, "Button1Click", "Click" );
   }
 public boolean dispatchEvent(Component component, String componentName, String eventName, Object[] params )
   {
      if (component.equals(Button1) && eventName.equals("Click")) {
         ButtonClick();int value = 1; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button2) && eventName.equals("Click")) {
         ButtonClick();int value = 2; txt +=String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button3) && eventName.equals("Click")) {
         ButtonClick();int value = 3; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button4) && eventName.equals("Click")) {
         ButtonClick();int value = 4; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button5) && eventName.equals("Click")) {
         ButtonClick();int value = 5; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button6) && eventName.equals("Click")) {
         ButtonClick();int value = 6; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button7) && eventName.equals("Click")) {
         ButtonClick();int value = 7; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button8) && eventName.equals("Click")) {
         ButtonClick();int value = 8; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button9) && eventName.equals("Click")) {
         ButtonClick();int value = 9; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Button0) && eventName.equals("Click")) {
         ButtonClick();int value = 0; txt += String.valueOf(value);tb.Text(txt);
         return true;
      }
      if (component.equals(Buttonplus) && eventName.equals("Click")) {
         ButtonClick();int buffer=0;buffer = Integer.parseInt(txt);
         String value = "+"; txt2 = txt+ String.valueOf(value);tb2.Text(txt2);
         txt = "";
         return true;
      }
      if (component.equals(Buttonminus) && eventName.equals("Click")) {
         ButtonClick();int buffer=0; buffer = Integer.parseInt(txt);
         String value = "-"; txt2 = txt+ String.valueOf(value);tb2.Text(txt2);
         txt = "";
         return true;
      }
      if (component.equals(Buttondivide) && eventName.equals("Click")) {
         ButtonClick();int buffer=0; buffer = Integer.parseInt(txt);
         String value = "/"; txt2 = txt+ String.valueOf(value);tb2.Text(txt2);
         txt = "";
         return true;
      }
      if (component.equals(Buttonmulti) && eventName.equals("Click")) {
         ButtonClick();int buffer=0; buffer = Integer.parseInt(txt);
         String value = "*"; txt2 = txt+ String.valueOf(value);tb2.Text(txt2);
         txt = "";
         return true;
      }
      if(component.equals(Buttonclear) && eventName.equals("Click")){
         ButtonClick();txt="";tb.Text(txt);
      }
      if(component.equals(Cal) && eventName.equals("Click")){
         ButtonClick();
         if(txt2.contains("+")){
         arr = txt2.split("\\+");
         result = Integer.parseInt(arr[0])+ Integer.parseInt(txt);
         tb2.Text(String.valueOf(result));}
         if(txt2.contains("-")){
            arr = txt2.split("\\-");
            result = Integer.parseInt(arr[0])- Integer.parseInt(txt);
            tb2.Text(String.valueOf(result));}
         if(txt2.contains("/")){
            arr = txt2.split("\\/");
            result2 = Double.parseDouble(arr[0])/ Double.parseDouble(txt);
            tb2.Text(String.valueOf(result2));}
         if(txt2.contains("*")){
            arr = txt2.split("\\*");
            result = Integer.parseInt(arr[0])* Integer.parseInt(txt);
            tb2.Text(String.valueOf(result));}
      }

    return false;
   }
public void ButtonClick()
   {
      Button1.GotFocus();
   }
   
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.example.acer.aibridgeapp">

    <application        
android:allowBackup="true"        
android:icon="@mipmap/ic_launcher"        
android:label="@string/app_name"        
android:roundIcon="@mipmap/ic_launcher_round"        
android:supportsRtl="true"        
android:theme="@style/AppTheme">
        <activity android:name=".Screen1">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




댓글

이 블로그의 인기 게시물

About JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

Spring Boot Actuator readiness, liveness probes on k8s

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째