Tra*_*eIt 4 android android-dialog
我是android的初学者,而不是为每个活动中的对话重复编写代码,我只是创建了一个包含显示对话框的所有方法的类,我已经给出了小代码片段
public class Dialogues extends Activity implements DialogueMethods {
public void showAlertDialog(Context context, String title, String message) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.show();
}
//this method am calling
public void showAlertDialog(Context context, String title, String message, String ButtonText, boolean cancel) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(title);
alertDialog.setMessage(message);
if(cancel) {
alertDialog.setNegativeButton(ButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
});
}
alertDialog.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我在打电话
//dialogObj is instance of the above class
dialogObj.showAlertDialog(MyActivity.this, "Error", "Not Connected to Internet", "Exit", true);
Run Code Online (Sandbox Code Playgroud)
当我运行代码对话框是可见但按钮不是,这是因为DialogInterace.onClickListener ?,我只是想知道这样做的好主意吗?如果是,那么这是正确的方法.请帮我.
谢谢.
只是我想分享我的方式,我是如何使用的.你可以上课,然后打电话到你想要的地方.
public class DialogsUtil {
private Context mContext;
public DialogsUtil(Context context) {
this.mContext = context;
}
/**
* Return an alert dialog
*
* @param message message for the alert dialog
* @param listener listener to trigger selection methods
*/
public void openAlertDialog(Context context, String message, String positiveBtnText, String negativeBtnText,
final OnDialogButtonClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setPositiveButton(positiveBtnText, (dialog, which) -> {
dialog.dismiss();
listener.onPositiveButtonClicked();
});
builder.setNegativeButton(negativeBtnText, (dialog, which) -> {
dialog.dismiss();
listener.onNegativeButtonClicked();
});
builder.setTitle(context.getResources().getString(R.string.app_name));
builder.setMessage(message);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setCancelable(false);
builder.create().show();
}
/**
* return a dialog object
* @return
*/
public Dialog openDialog(@LayoutRes int layoutId) {
Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(layoutId);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
}
Run Code Online (Sandbox Code Playgroud)
我为这个对话框创建了一个界面,
public interface OnDialogButtonClickListener {
void onPositiveButtonClicked();
void onNegativeButtonClicked();
}
Run Code Online (Sandbox Code Playgroud)
只需在您想要使用对话框的活动中实现此接口,并在类对象的帮助下,您可以使用这样的对话框,
mDialogsUtil.openAlertDialog(YourActivity.this, "text message", "positive button msg", "Negative button msg", this);
Run Code Online (Sandbox Code Playgroud)
你可以在你的活动中覆盖这两种方法,
@Override
public void onPositiveButtonClicked() {
}
//user clicked cancel.Close the application
@Override
public void onNegativeButtonClicked() {
}
Run Code Online (Sandbox Code Playgroud)
谢谢希望这会对你有所帮助.