안드로이드 EditText외에 다른 영역을 선택 했을 때 키보드를 제거 하는 코드이다.

 

TouchedView ! is Button 조건을 사용하지 않고 dispatchTouch event만 사용했을 경우 키보드가 뜬 상태로 버튼 선택이 되지 않는 SideEffet가 발생하여 선택한 영역이 버튼일 경우에는 제외하는 조건을 추가 하였다.

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val focusView = currentFocus

        if (focusView != null) {
            val rect = Rect()
            focusView.getGlobalVisibleRect(rect)
            val x = ev.x.toInt()
            val y = ev.y.toInt()

            // 터치된 뷰가 버튼인지 확인
            val touchedView = findViewAtPosition(x, y)
            if (!rect.contains(x, y) && touchedView !is Button) {
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(focusView.windowToken, 0)
                focusView.clearFocus()
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun findViewAtPosition(x: Int, y: Int): View? {
        return window.decorView.findViewById<ViewGroup>(android.R.id.content)?.let { rootView ->
            findViewAtPosition(rootView, x, y)
        }
    }

    private fun findViewAtPosition(parent: ViewGroup, x: Int, y: Int): View? {
        for (i in 0 until parent.childCount) {
            val child = parent.getChildAt(i)
            if (child is ViewGroup) {
                val foundView = findViewAtPosition(child, x, y)
                if (foundView != null) {
                    return foundView
                }
            } else {
                val rect = Rect()
                child.getGlobalVisibleRect(rect)
                if (rect.contains(x, y)) {
                    return child
                }
            }
        }
        return null
    }
    private fun deviceId(): String? {
        val deviceIdKey = "makeDeviceID"
        val sharedPreferences = getSharedPreferences("AppDate", 0)
        var deviceId = sharedPreferences.getString(deviceIdKey, null)
        if (deviceId == null) {
            deviceId = retrieveDeviceId()
            if (deviceId == null) {
                deviceId = retrieveAndroidIdOrGenerateUUID()
            }
            val editor = sharedPreferences.edit()
            editor.putString(deviceIdKey, deviceId)
            editor.commit()
        }
        return deviceId
    }


private fun retrieveDeviceId(): String? {
    return try {
        val telephonyManager =
            this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (Build.VERSION.SDK_INT < 26) {
            telephonyManager.deviceId
        } else {
            telephonyManager.imei
        }
    } catch (e: Exception) {
        // Handle exception (log if necessary)
        null
    }
}

private fun retrieveAndroidIdOrGenerateUUID(): String? {
    try {
        val androidId = Settings.Secure.getString(
            this.contentResolver,
            Settings.Secure.ANDROID_ID
        )
        if (androidId != null && androidId.isNotEmpty()) {
            return androidId
        }
    } catch (e: Exception) {
        // Handle exception (log if necessary)
    }
    return UUID.randomUUID().toString().replace("-".toRegex(), "")
}

TypeScript vs JavaScript

Static Types(set during development) - 오류를 개발하는 중간에 체크 vs Dynamic types(resolved at runtime) - 오류를 런타임을 하여야 알수 있음

 

프로그램이 유용하려면, 가장 간단한 데이터 단위로 작업 할 수 있어야 하며, TypeScript에서는 JavaScript에서 기대하는 것과 동일한 타입을 지원하기 위해 추가적인 열거 타입이 제공된다.

 

TypeScript에서 프로그램 작성을 위해 제공되는 기본타입 

JavaScript 기본 자료형을 포함(Superset), ECMAScript 표준에 따른 기본 자료형 6가지

 

- boolean, number, string, null, undefined, symbol(ES6이후에 추가)

- Array(Object 형)

- 프로그래맹을 도울 몇 가지 타입(any, void, never, unknown, enum)

 

Primitive types

오브젝트와 레퍼런스형태가 아닌 실제 값을 저장하는 자료형을 말한다. 즉 메모리에 주소가 아닌 선언된 자료형 그대로 데이터를 저장하고 있는 자료형을 말하며, 자바스크립트 처리 방식으로 Primitive 형의 내장 함수를 사용가능하다. 

 

 

 

 

'TypeScript' 카테고리의 다른 글

[TypeScript] 타입스크립트란?  (0) 2023.01.16

+ Recent posts