2021. 1. 21. 1:19
https://blog.naver.com/nagne2011/222214424367
안드로이드 Fragment - Dialog구현
#안드로이드 #Fragment #dialog 프래그먼트 사용시 안드로이드의 '팝업' UI인 dialog 구현 방...
blog.naver.com
#안드로이드 #Fragment #dialog

사진 설명을 입력하세요.
프래그먼트 사용시 안드로이드의 '팝업' UI인 dialog 구현 방법이다
시작은 xml을 만드는것부터 시작하자
기본적으로 '확인' 버튼만 있으니 간단히 배경이미지와 확인버튼 정도만 추가하자
<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">
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="373dp"
android:layout_marginStart="11dp"
android:layout_marginTop="142dp"
android:layout_marginEnd="11dp"
android:src="@drawable/default_box"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_dialog_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="104dp"
android:text="confirm"
app:layout_constraintBottom_toBottomOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
(귀찮으므로 대충 드래그해서 배치..)
이제 dialog를 위한 새로운 클래스를 만든다
public class DialogFragment extends androidx.fragment.app.DialogFragment {
public static final String TAG_EVENT_DIALOG = "dialog_event";
private Button btn_confirm;
public DialogFragment() { }
public static DialogFragment getInstance() {
DialogFragment e = new DialogFragment();
return e;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle saveInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, container);
btn_confirm = (Button)view.findViewById(R.id.btn_dialog_confirm);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
}
extend는 androidx.fragment.app.DialogFragment를 상속받아 생성해주고
기존에 만든 xml과 연결, onCreateView에 버튼을 생성해서 xml에 만들었떤 버튼도 연결해준다
그리고 '확인'버튼을 누를시 dismiss()가 동작하게 하자
여기서 dismiss()함수는 dialogFragment의 기본 함수로 cancel()동작 이후 동작하는 함수며
안드로이드의 back버튼을 눌렀을때 dialog를 종료하는 함수로도 쓰인다
자세한건 링크참고
안드로이드(Android) Dialog의 cancel()과 dismiss()의 차이 Posted on 2011년 12월 7일 by mcsong with 2 Comments 안드로이드(Android)에서 다이얼로그(Dialog)는 앱이 자주 사용하는 UI 컴포넌트이다. 다이얼로그를 보여주는 데는 show() 세드를 사용하고, 종료하는데는 cancel(), dismiss() 메서드를 사용할 수 있다. 종료하는 이 두 메서드에 대해서 API 문서에서는 아래와 같이 설명하고 있다. UI 경험이 별로 없다보니 잘 감이 안 온다. 화면에서 다...
sjava.net
마지막으로 생성한 dialog를 메인 엑티비티에 사용해보자
public void showDialog() {
DialogFragment e = DialogFragment.getInstance();
e.show(m_fragmentManager, DialogFragment.TAG_EVENT_DIALOG);
}
완성!
'안드로이드' 카테고리의 다른 글
안드로이드 svg 리소스 (0) | 2022.02.15 |
---|---|
안드로이드 fragment 권한설정(permission) (0) | 2022.02.15 |
안드로이드 Fragment - 초기화, 이동(replace, add, remove) (0) | 2022.02.15 |
안드로이드 영상통화 peerjs - 3 - activity_call (0) | 2022.02.15 |
안드로이드 영상통화 peerjs - 2 - firebase (0) | 2022.02.15 |