2019-07-22 23:53:44 Java

Java

Copy Copied! Full
import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Log; import androidx.fragment.app.DialogFragment; import static android.provider.Settings.Global.getString; public class DialogClass extends DialogFragment { String title,message; Context context; Type type; Boolean select; public enum Type{ yes, no }; DialogInterface.OnClickListener onClickPositive; DialogInterface.OnClickListener onClickNegative; public DialogClass(){ } public Dialog onCreateDialog(Bundle savedInstanceState){ AlertDialog.Builder builder = new AlertDialog.Builder(context); switch (type) { case yes: builder.setTitle(title) .setMessage(message) .setPositiveButton(context.getString(R.string.yes), onClickPositive); case no: builder.setTitle(title) .setMessage(message) .setPositiveButton(context.getString(R.string.yes), onClickPositive) .setNegativeButton(context.getString(R.string.no), onClickNegative); } return builder.create(); } public boolean getresult(){ return select; } public void onCancel(DialogInterface dialog) { Intent intent = new Intent(context, MainActivity.class); context.startActivity(intent); } public void setTitle(String title){ this.title=title; } public void setMessage(String message){ this.message=message; } public void setType(Type type){ this.type=type; } public void setContext(Context context){ this.context=context; } } /* 呼び出し部分 */ dialogClass.setTitle(""); dialogClass.setMessage(""); dialogClass.setType(DialogClass.Type.no); dialogClass.setContext(this.getApplicationContext()); dialogClass.onClickPositive = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Snackbar.make(container, "OnClick 'OK'", Snackbar.LENGTH_SHORT).show(); } }; dialogClass.onClickNegative = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Snackbar.make(container, "OnClick 'cancel'", Snackbar.LENGTH_SHORT).show(); } }; dialogClass.show(supportFragmentManager, "button");
RECOMMEND