앱 만들기/안드로이드

리스트형 기본 다이얼로그 만들기

나도 처음이야 2022. 7. 17.

https://soo0100.tistory.com/1900

 

기본 다이얼로그 박스 만들기

안드로이드 Alert Dialog 라고 불리는 기본 다이얼로그를 만들어 봅니다. 결과는 하기와 같습니다. 1. XML 화면구성으로 버튼을 하나 만듭니다.  해당 버튼을 누르면 다이얼로그가 나오게 구현하기

soo0100.tistory.com

Alert 기본 다이얼로그 에서 하기 처럼 리스트형을 만들어 봅니다.

지난 시간과 XML 및 코드 구성은 동일합니다.

다만, 해당 리스트 형식을 위해서 신규 함수를 만들어 버튼뷰의 Onclick 이벤트에 적용시킵니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="AlertDialogListType"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="AlertDialogListType"
        android:text="Alert Dialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. MainActivity 에 onClick 호출시 불려지는 함수를 추가합니다.

    해당 함수 내에서 리스트형 alert Dialog 를 구현 해 봅니다.

final CharSequence[] items ={"제주", "프랑크푸르트" , "파리"};
//list 타입의 다이얼로 박스
public void AlertDialogListType(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("목적지를 선택하세요");
    builder.setItems(items, new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialogInterface, int num) {
            Toast.makeText(getApplicationContext(), items[num], Toast.LENGTH_LONG).show();
        }
    });
    builder.show();
}

builder.setItems() 함수를 사용해서, 리스트 메뉴를 만드는 동시에 익명클래스를 활용해서 클릭 리스너를 통합적으로 구현 해줍니다.

위에서는 각 리스트 별로, 간단한 토스트 메시지만 띄웠지만 상황에 맞게 구현을 하면 되겠습니다.

감사합니다. 

https://soo0100.tistory.com/1902

 

체크박스 형 기본 다이얼로그 만들기

https://soo0100.tistory.com/1901 리스트형 기본 다이얼로그 만들기 https://soo0100.tistory.com/1900 기본 다이얼로그 박스 만들기 안드로이드 Alert Dialog 라고 불리는 기본 다이얼로그를 만들어 봅니다. 결..

soo0100.tistory.com

 

반응형

댓글