컴퓨터공부/Android

CustomDialog Fragment 예제

achivenKakao 2017. 9. 7. 00:41


DialogFragment 를 이용해서 팝업이 뜨는 것을 구현해 봤다..
아래 구문은 다음에 쓰일 수도 있을 것 같아서 표시해둔다.

    * DialogFragment를 호출하는 것이 Fragment일 때, support 버전을 맞춰줘야지 컴파일 문제가 없어진다.
      즉, android.support.v7.app의 Fragment는 android.support.v7.app.DialogFragment의 DialogFragment를 호출해야 하고,
  android.support.v4.app의 Fragment는 android.support.v4.app.DialogFragment의 DialogFragment를 호출해야 함
Fragment prev = fm.findFragmentByTag(DIALOG_TAG);



package com.study.practice.dialogfragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;

public class MainActivity extends AppCompatActivity {

static final String DIALOG_TAG = "DIALOGTAG";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager fm = getSupportFragmentManager();

ServerTestDialogFragment dialog = new ServerTestDialogFragment();
dialog.show(fm, DIALOG_TAG);

//
// FragmentTransaction tr = fm.beginTransaction();
// Fragment prev = fm.findFragmentByTag(DIALOG_TAG);
//
// if(prev != null){
// tr.remove(prev);
// }


}

} 






 package com.study.practice.dialogfragment;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

/**
* Created by kimjeongho on 2017. 9. 6..
*/

public class ServerTestDialogFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

//Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialogfragment_main, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// sign in the user
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ServerTestDialogFragment.this.getDialog().cancel();
}
});

return builder.create();
}
}



dialogfragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textEmailAddress"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:fontFamily="sans-serif"
android:inputType="textPassword"
/>

</LinearLayout> 





출처 : 


https://developer.android.com/guide/topics/ui/dialogs.html?hl=ko
http://gakari.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-DialogFragment-%EB%A1%9C-%EB%8C%80%ED%99%94%EC%83%81%EC%9E%90-%EB%A7%8C%EB%93%A4%EA%B8%B0