728x90

안드로이드 View Binding 기능을 어떤 걸로 테스트하면 좋을까 생각하다가 GitHub 에서 계산기를 받아서 그걸로 View Binding 테스트를 했다.


뷰 바인딩은 view와 상호작용하는 코드를 더욱 쉽게 작성할 수 있도록 도와주는 기능이다.


View Binding 을 하는 방법

1. 앱 build.gradle 추가사항

    viewBinding {
        enabled = true
    } 


2. Java code 수정사항

setContentView(R.layout.activity_main); 대신에

private ActivityMainBinding binding;

binding = ActivityMainBinding.inflate(getLayoutInflater()); // 1. inflate 호출하여 인스턴스 생성

View view = binding.getRoot(); // 2. getRoot()

setContentView(view); // 3. 화면상의 활성 뷰로 만든다.


binding.btnClear.setOnClickListener(v -> {
    binding.tvInput.setText("");
    binding.tvOutput.setText("");
});


3. XML 파일 수정사항은 없다.

    - XML 파일을 분리하여 호출하는 구조로는 binding 을 인식하지 못하더라. 그래서 1개의 파일로 작성했음.

    - findViewById 호출 대신에 직접 id값을 binding 한다.


<Button
    android:id="@+id/btnClear"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_row="0"
    android:layout_column="0"
    android:backgroundTint="@color/colorWhiteGray"
    android:text="CL"
    android:textColor="@color/colorBlack"
    android:textSize="25sp" />


계산기 예제코드

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    viewBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'com.google.android.material:material:1.2.1'
    // Rhino is an open-source implementation of JavaScript written entirely in Java.
    implementation group: 'org.mozilla', name: 'rhino', version: '1.7.13'

}


import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.link2me.android.calculator.databinding.ActivityMainBinding;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class MainActivity extends AppCompatActivity {
    String process;
    Boolean checkBracket = false;

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater()); // 1. inflate 호출하여 인스턴스 생성
        View view = binding.getRoot(); // 2. getRoot()
        setContentView(view); // 3. 화면상의 활성 뷰로 만든다.

        binding.btnClear.setOnClickListener(v -> {
            binding.tvInput.setText("");
            binding.tvOutput.setText("");
        });

        binding.btn0.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "0");
        });

        binding.btn1.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "1");
        });

        binding.btn2.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "2");
        });

        binding.btn3.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "3");
        });

        binding.btn4.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "4");
        });

        binding.btn5.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "5");
        });

        binding.btn6.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "6");
        });

        binding.btn7.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "7");
        });

        binding.btn8.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "8");
        });

        binding.btn9.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "9");
        });

        binding.btn0.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "0");
        });

        binding.btnAdd.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "+");
        });

        binding.btnMinus.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "-");
        });

        binding.btnMultiply.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "×");
        });

        binding.btnDivision.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "÷");
        });

        binding.btnDot.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + ".");
        });

        binding.btnPercent.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();
            binding.tvInput.setText(process + "%");
        });

        binding.btnEquals.setOnClickListener(v -> {
            process = binding.tvInput.getText().toString();

            process = process.replaceAll("×", "*");
            process = process.replaceAll("%",  "/100");
            process = process.replaceAll("÷","/");

            Context rhino = Context.enter();
            rhino.setOptimizationLevel(-1);

            String finalResult = "";

            try {
                Scriptable scriptable = rhino.initStandardObjects();
                finalResult = rhino.evaluateString(scriptable,process,"javascript",1,null).toString();
            }catch (Exception e){
                finalResult="0";
            }
            binding.tvOutput.setText(finalResult);
        });

        binding.btnBracket.setOnClickListener(v -> {
            if (checkBracket){
                process = binding.tvInput.getText().toString();
                binding.tvInput.setText(process + ")");
                checkBracket = false;
            }
            else{
                process = binding.tvInput.getText().toString();
                binding.tvInput.setText(process + "(");
                checkBracket = true;
            }
        });

    }
}


전체 코드는 https://github.com/jsk005/JavaProjects/tree/master/calculator 에 있다.

728x90
블로그 이미지

Link2Me

,