以前、DialogFragment はインナークラスで書くべきかと悩んだが、
そのようにどちらかだけにする考えがナンセンスだと気づいた。
単純なメッセージを出すダイアログなら、以下のように、一度、外側に宣言する DialogFragment を書けば、
インナークラスでも同じように書ける。
インナークラスでも使えるようにするために、Fragment に渡すパラメータキーを、interface 定義してしまうのがポイントです。
import java.io.Serializable;
/**
* SimpleDialogInterface
*/
public interface SimpleDialogInterface extends Serializable{
// android.os.Bundle に渡すパラメータ key
public final static String MESSAGE = "Message";
public final static String TITLE = "Title";
public final static String POSITIVE_LABEL = "PositiveLabel";
public final static String NEGATIVE_LABEL = "NegativeLabel";
public final static String NEUTRAL_LABEL = "NeutralLabel";
public final static String LISTENER = "Listener";
public final static int POSITIVE = 0;
public final static int NEGATIVE = 1;
public final static int NEUTRAL = 2;
/**
* Dialog Button on Click
* @param which 各ボタンが押された時の判断としてこのインターフェースで定義した値を返す
* PositiveButton → 0 = SimpleDialogInterface.POSITIVE
* NegativeButton → 1 = SimpleDialogInterface.NEGATIVE
* NeutralButton → 2 = SimpleDialogInterface.NEUTRAL
*/
public void onButtonClick(int which);
}
==============================
DialogFragment のクラスの定義
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* SimpleMessageDialog
*/
public class SimpleMessageDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Bundle bundle = getArguments();
builder.setTitle(bundle.getString(SimpleDialogInterface.TITLE, null));
String message = bundle.getString(SimpleDialogInterface.MESSAGE, null);
if (message != null) builder.setMessage(message);
String positiveLabel = bundle.getString(SimpleDialogInterface.POSITIVE_LABEL, null);
String negativeLabel = bundle.getString(SimpleDialogInterface.NEGATIVE_LABEL, null);
String neutralLabel = bundle.getString(SimpleDialogInterface.NEUTRAL_LABEL, null);
if (positiveLabel != null || negativeLabel != null || neutralLabel != null){
final SimpleDialogInterface simpleface = (SimpleDialogInterface)getArguments().getSerializable(SimpleDialogInterface.LISTENER);
if (positiveLabel != null){
builder.setPositiveButton(positiveLabel, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
simpleface.onButtonClick(SimpleDialogInterface.POSITIVE);
}
});
}
if (negativeLabel != null){
builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
simpleface.onButtonClick(SimpleDialogInterface.NEGATIVE);
}
});
}
if (neutralLabel != null){
builder.setNeutralButton(neutralLabel, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
simpleface.onButtonClick(SimpleDialogInterface.NEUTRAL);
}
});
}
}
return builder.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return null;
}
}
============================================
呼び出しのサンプル
Bundle args = new Bundle();
args.putString(SimpleDialogInterface.TITLE, "Test");
args.putString(SimpleDialogInterface.MESSAGE, "message");
args.putString(SimpleDialogInterface.POSITIVE_LABEL, "OK");
args.putString(SimpleDialogInterface.NEGATIVE_LABEL, "Cancel");
args.putSerializable(SimpleDialogInterface.LISTENER, new SimpleDialogInterface(){
@Override
public void onButtonClick(int which){
String s = "";
switch(which){
case SimpleDialogInterface.POSITIVE:
s = "OK press";
break;
case SimpleDialogInterface.NEGATIVE:
s = "Cancel press";
break;
default:
return;
}
Toast.makeText(getApplicationContext(), "Click "+s, Toast.LENGTH_SHORT).show();
}
});
DialogFragment df = new SimpleMessageDialog();
df.setArguments(args);
df.show(getFragmentManager(), "tag");