컴퓨터공부/Android

CustomDialogOnAttach Fragment 예제

achivenKakao 2017. 10. 13. 01:08

CustomDialog Fragment 예제의 연장선이다.

그 전 예제는 그저 dialog만 띄우고 끝이 났다.


이제는 dialog를 띄우고 '확인', '취소' 중에 어떤 것을 선택했는지 확인하는 예제이다.

(https://developer.android.com/guide/topics/ui/dialogs.html?hl=ko#PassingEvents 중 "이벤트를 대화상자의 호스트에 다시 전달" 참조)



DialogOnAttachFragment.zip



ServerTestDialogFragment.java



public class ServerTestDialogFragment extends DialogFragment {

public interface DialogListener{
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}

DialogListener mListener;

@Override
public void onAttach(Context context) {
super.onAttach(context);

// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (DialogListener) getActivity();
}
catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(getActivity().toString()
+
" must implement NoticeDialogListener");
}
}

@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 // null은 사실 테마와 관련이 있다!!(https://stackoverflow.com/questions/2422562/how-to-change-theme-for-alertdialog)

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

mListener.onDialogPositiveClick(ServerTestDialogFragment.this);

}
})
.setNegativeButton(R.string.
cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(ServerTestDialogFragment.this);
//ServerTestDialogFragment.this.getDialog().cancel();
}
});

return builder.create();
}

} 


> interface를 정의하고 클릭했을 때 호출 하도록 한다.




MainActivity.java



public class MainActivity extends AppCompatActivity implements ServerTestDialogFragment.DialogListener{

static final String TAG = "DIALOG_ONATTACH";

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

Button btn = (Button) findViewById(R.id.button);


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(TAG);

if(prev != null){
tr.remove(prev);
}

ServerTestDialogFragment dialog = new ServerTestDialogFragment();
dialog.show(fm, TAG);
}
});


}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
if(dialog != null && dialog.getShowsDialog()){
dialog.dismiss();
Toast.makeText(this, "확인", Toast.LENGTH_SHORT).show();

}
}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {
if(dialog != null && dialog.getShowsDialog()){
dialog.dismiss();
Toast.makeText(this, "취소", Toast.LENGTH_SHORT).show();
}
}

} 


> interface를 구현해주면 끝난다. 이전 예제에 비해서 버튼을 클릭하면 팝업이 뜨도록 변경하였다.




출처 : https://developer.android.com/guide/topics/ui/dialogs.html?hl=ko#PassingEvents