DialogFragmentでシンプルで汎用的なダイアログ
package sakura.example.myexamdialogfragment; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
public class CommonDialogFragment extends DialogFragment { | |
private DialogListener listener = null; | |
public static CommonDialogFragment newInstance(String title, String message) { | |
CommonDialogFragment frag = new CommonDialogFragment(); | |
Bundle bundle = new Bundle(); | |
bundle.putString("title", title); | |
bundle.putString("message", message); | |
frag.setArguments(bundle); | |
return frag; | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
String title = getArguments().getString("title"); | |
String message = getArguments().getString("message"); | |
return new AlertDialog.Builder(getActivity()) | |
.setIcon(R.drawable.ic_launcher) | |
.setTitle(title) | |
.setMessage(message) | |
.setPositiveButton(android.R.string.ok, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int whichButton) { | |
listener.onPositiveClick(); | |
} | |
} | |
) | |
.setNegativeButton(android.R.string.cancel, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int whichButton) { | |
listener.onNegativeClick(); | |
} | |
} | |
) | |
.create(); | |
} | |
/** | |
* リスナーを追加する | |
* | |
* @param listener | |
*/ | |
public void setDialogListener(DialogListener listener) { | |
this.listener = listener; | |
} | |
/** | |
* リスナーを削除する | |
*/ | |
public void removeDialogListener() { | |
this.listener = null; | |
} | |
} |
package sakura.example.myexamdialogfragment; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentTransaction; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class Fragment01 extends Fragment implements OnClickListener, DialogListener { | |
// DialogListenerを実装しておく | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
if (container == null) { | |
return null; | |
} | |
return inflater.inflate(R.layout.fragment01, container, false); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
Button button = (Button) getView().findViewById(R.id.button1); | |
button.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.button1: | |
// バックスタックに以前のFragmentがあれば取り除く | |
FragmentTransaction ft = getFragmentManager().beginTransaction(); | |
Fragment prev = getFragmentManager().findFragmentByTag("CommonDialogFragment"); | |
if (prev != null) { | |
ft.remove(prev); | |
} | |
ft.addToBackStack(null); | |
// Create and show the dialog. | |
CommonDialogFragment newFragment = CommonDialogFragment.newInstance( | |
"title", "this is message"); | |
newFragment.setDialogListener(this); | |
newFragment.show(ft, "CommonDialogFragment"); | |
if (newFragment.isVisible()) { | |
newFragment.dismiss(); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
@Override | |
public void onPositiveClick() { | |
Toast.makeText(getActivity(), "ok ボタンが押された", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNegativeClick() { | |
Toast.makeText(getActivity(), "cancel ボタンが押された", Toast.LENGTH_SHORT).show(); | |
} | |
} |
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/root" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="ダイアログを表示する" /> | |
</RelativeLayout> |
package sakura.example.myexamdialogfragment; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentTransaction; | |
public class MainActivity extends FragmentActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Fragment01 frag = new Fragment01(); | |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | |
ft.add(R.id.main, frag, "Fragment01"); | |
ft.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment