Coding/Java 삽질기2021. 10. 24. 18:08

TextView에서 한 줄에 표시되는 글자수를 계산 하는 코드

var textView: TextView = findViewById(R.id.textView)
val COL : Int = Math.round(textView.width / textView.paint.measureText("가나다라마.") * 6)

그런데 OnCreate에서 위 함수를 사용하면 0이 나온다.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.textviewer)
      
        val vto: ViewTreeObserver = textView.getViewTreeObserver()
        vto.addOnGlobalLayoutListener {

            if (getWidthCount < 1) {
                getWidthCount++

                val COL : Int = Math.round(textView.width / textView.paint.measureText("가나다라마.") * 6)
                Log.i(TAG, "getWidth:" + COL)
            }
        }
    }

이 경우 위와 같이, 코드를 추가해 주면 된다.

Posted by chobocho
Coding/Java 삽질기2021. 10. 17. 13:38

Unit test에서 Test sample 파일을 읽어서 테스트 하는 방법

1. test 폴더에 resources 폴더를 만들고 테스트 파일을 넣는다

2. 아래와 같이 코드를 작성한다.

    @Test
    public void encrypt() throws URISyntaxException {
        File file = new File(getClass().getResource("/1.txt").toURI());
        assert(file != null);
        URL unavailableURL = getClass().getResource("/unavilable.txt");
        assert(unavailableURL == null);
    }

Posted by chobocho
Coding/JavsScript 삽질기2021. 10. 16. 13:54
Coding/Tip2021. 10. 16. 13:52

오늘날짜 입력: Ctrl + ;

'Coding > Tip' 카테고리의 다른 글

[DOSBOX] 창 크기 조절 방법  (0) 2023.01.27
[Android studio] 사용 팁  (0) 2020.04.13
사무직을 위한 Git 활용 법  (0) 2018.10.27
Posted by chobocho