투케이2K

545. (kotlin/코틀린) [간단 소스] 안드로이드 휴대폰 번호 입력 팝업창 UI 유틸 파일 제작 - 커스텀 Alert 팝업창 본문

Kotlin

545. (kotlin/코틀린) [간단 소스] 안드로이드 휴대폰 번호 입력 팝업창 UI 유틸 파일 제작 - 커스텀 Alert 팝업창

투케이2K 2025. 2. 22. 11:25

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Kotlin

 

[소스 코드]

// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------

- 언어 : Java / Kotlin

- 개발 툴 : AndroidStudio

- 기술 구분 : Alert / 커스텀 / 휴대폰 번호 입력 팝업창

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[xml 소스 코드]
// --------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main__door_guard__main_search_setting_state"
    android:layout_width="match_parent"
    android:layout_height="245dp"
    android:orientation="vertical"
    android:background="#ffffff"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="회원 가입 SMS 문자 전송"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="end"
        android:background="#19295a"
        android:layout_marginBottom="5dp"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:lineSpacingExtra="5dp"
        android:text="회원 가입 SMS 문자 전송을 하기 위한\n휴대폰 번호를 입력해주세요."
        android:textColor="#000000"
        android:textSize="13sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:orientation="vertical"
        android:background="#000">
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="※ 휴대폰 번호 입력 시 특수 문자 없이 입력해주세요.\n휴대폰 번호 입력 예시 : 01012345678"
        android:lineSpacingExtra="5dp"
        android:textStyle="bold"
        android:textColor="#ff0033"
        android:textSize="13sp"
        android:gravity="center"
        android:layout_margin="5dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:orientation="vertical"
        android:background="#000">
    </LinearLayout>

    <EditText
        android:id="@+id/phoneEditText"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text=""
        android:hint=""
        android:textStyle="bold"
        android:textColor="#19295a"
        android:textSize="13sp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="end"
        android:padding="0dp"
        android:layout_margin="2dp"
        android:background="#ccc"/>

</LinearLayout>

// -----------------------------------------------------------------------------------------






// -----------------------------------------------------------------------------------------
[Kotlin : 소스 코드]
// -----------------------------------------------------------------------------------------

fun observablePhoneInput(mContext: Context, phoneNumber: String, ok: String, no: String): Observable<String> {

    // [로직 처리 실시]
    return Observable.create<String> { subscriber: ObservableEmitter<String> ->
        try {

            // [UI 생성 실시]
            val inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val dialogView: View = inflater.inflate(R.layout.view_custom_alert_phone_input, null) // [xml 지정]
            val phoneEditText = dialogView.findViewById<View>(R.id.phoneEditText) as EditText

            var className = ""
            try { className = Thread.currentThread().stackTrace[3].toString() } catch (ec: Exception) { }


            // [로그 쌓기]
            try {
                S_Log._F_(mContext, "observablePhoneInput : 휴대폰 번호 입력 팝업창 표시", arrayOf(
                    "CLASS : $className",
                    "PHONE_NUMBER : $phoneNumber"
                ))
            } catch (ez: Exception) {
            }


            // [팝업창 생성 실시]
            Handler(Looper.getMainLooper()).postDelayed({

                val alertDialog: AlertDialog?

                // [AlertDialog 팝업창 생성]
                val builder = AlertDialog.Builder(mContext)
                //builder.setTitle(title); //[팝업창 타이틀 지정]
                //builder.setIcon(R.drawable.icon); //[팝업창 아이콘 지정]
                //builder.setMessage(ment); //[팝업창 내용 지정]
                builder.setCancelable(false) //[외부 레이아웃 클릭시도 팝업창이 사라지지않게 설정]
                builder.setView(dialogView) // TODO [뷰 지정]
                builder.setPositiveButton(ok) { dialog, which ->
                    // -----------------------------------------
                    // TODO [확인 버튼 클릭 이벤트 처리]
                    // -----------------------------------------
                }
                builder.setNegativeButton(no) { dialog, which ->
                    // -----------------------------------------
                    try { S_Log._F_(mContext, "observablePhoneInput : [" + no + "] 버튼 클릭 이벤트 발생 : " + C_Util.getSourceCodeLine(), null) } catch (ez: Exception) { }
                    // -----------------------------------------


                    // -----------------------------------------
                    // TODO [키보드 내림]
                    // -----------------------------------------
                    //*
                    try {
                        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        imm.hideSoftInputFromWindow(phoneEditText.windowToken, 0)
                    } catch (e: Exception) {
                    }
                    // */
                    // -----------------------------------------


                    // -----------------------------------------
                    // TODO [리턴 데이터 반환]
                    // -----------------------------------------
                    try {
                        subscriber.onNext("")
                        subscriber.onComplete()
                    } catch (ex: Exception) {
                        ex.printStackTrace()
                    }
                    // -----------------------------------------
                }
                alertDialog = builder.create()
                alertDialog.show()


                // -------------------------------------------
                // [확인 버튼 재상속 조건이 맞는 경우만 dismiss]
                // -------------------------------------------
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
                    .setOnClickListener {
                        // -----------------------------------------
                        // TODO [뷰에 표시된 정보 얻어온다] : 특수 문자 Replace 처리
                        // -----------------------------------------
                        var inputPhone = phoneEditText.text.toString()
                        inputPhone = inputPhone.replace(":".toRegex(), "")
                        inputPhone = inputPhone.replace("-".toRegex(), "")
                        inputPhone = inputPhone.replace(" ".toRegex(), "")

                        //inputPhone = C_Util.getRexpNumberString(inputPhone); // TODO [정규식 숫자만 지정]


                        // -----------------------------------------
                        try {
                            S_Log._F_(mContext, "observablePhoneInput : [" + ok + "] 버튼 클릭 이벤트 발생 : " + C_Util.getSourceCodeLine(), arrayOf(
                                "입력 한 휴대폰 번호 : " + phoneEditText.text.toString(),
                                "휴대폰 번호 정규식 수행 : $inputPhone"
                            ))
                        } catch (ez: Exception) {
                        }
                        // -----------------------------------------


                        // -----------------------------------------
                        // TODO [방어 로직] : 입력 정보 Null 체크
                        // -----------------------------------------
                        if (C_Util.stringNotNull(inputPhone) === false) {
                            toastLongRed(mContext, "※ 휴대폰 입력 정보가 올바르지 않습니다. (Data Is Null)\n\n정규식 수행 폰 번호 : $inputPhone")
                            return@setOnClickListener
                        }


                        // -----------------------------------------
                        // TODO [방어 로직] : 입력 된 EditText 글자 수 확인 : 자릿수 초과
                        // -----------------------------------------
                        if (inputPhone.length >= 15) {
                            toastLongRed(mContext, "※ 휴대폰 입력 정보가 올바르지 않습니다. (Data Length Over)\n\n정규식 수행 폰 번호 : $inputPhone")
                            return@setOnClickListener
                        }


                        // -----------------------------------------
                        // TODO [방어 로직] : 입력 된 EditText 글자 수 확인 : 자릿수 부족
                        // -----------------------------------------
                        if (inputPhone.length < 11) {
                            toastLongRed(mContext, "※ 휴대폰 입력 정보가 올바르지 않습니다. (Data Length Minor)\n\n정규식 수행 폰 번호 : $inputPhone")
                            return@setOnClickListener
                        }


                        // -----------------------------------------
                        // TODO [리턴 데이터 반환]
                        // -----------------------------------------
                        try {
                            subscriber.onNext(inputPhone)
                            subscriber.onComplete()
                        } catch (ex: Exception) {
                            ex.printStackTrace()
                        }
                        // -----------------------------------------


                        // -----------------------------------------
                        // TODO [키보드 내림]
                        // -----------------------------------------
                        //*
                        try {
                            val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(phoneEditText.windowToken, 0)
                        } catch (e: Exception) {
                        }
                        // */
                        // -----------------------------------------


                        // -----------------------------------------
                        // TODO [Alert 팝업창 닫기]
                        // -----------------------------------------
                        if (alertDialog != null){
                            alertDialog?.dismiss()
                        }
                        // -----------------------------------------

                    }
            }, 0)

        } catch (e: Exception) {
            e.printStackTrace()

            // ------------------------------------------------------
            // TODO [리턴 데이터 반환]
            // ------------------------------------------------------
            try {
                subscriber.onNext("")
                subscriber.onComplete()
            } catch (ex: Exception) {
                ex.printStackTrace()
            }
        }
    }
}

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------

[raw 폴더 , menu 폴더 , anim 폴더 생성 방법 설명]

https://blog.naver.com/kkh0977/222356039600?trackingCode=blog_bloghome_searchlist


[커스텀 다이얼로그 (custom dialog alert) 팝업창 만들기]

https://blog.naver.com/kkh0977/222590922634?trackingCode=blog_bloghome_searchlist

// --------------------------------------------------------------------------------------
 
반응형
Comments