具有默认主题的自定义 AlertDialog
Custom AlertDialog with default theme
我正在尝试使用默认主题构建自定义 AlertDialog
,以便我可以在代码中的多个位置使用相同的 AlertDialog
。我通过扩展 AlertDialog
并编写我的自定义业务逻辑来决定对话框中的消息来创建 class。问题是,当我调用 AlertDialog.show()
时,我看到一个没有显示对话框的黑色覆盖层。这是我的代码:
public class ClosestShrutiDialog extends AlertDialog {
private String mLessonId;
private Activity mActivity;
private View mParentView;
private OnClickedListener mOnClickListerner;
public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
super(activity);
this.mLessonId = lessonId;
this.mActivity = activity;
this.mParentView = parentView;
}
public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
this.mOnClickListerner = mOnClickListerner;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// My business login to decide the msg related params ....
// showing message to user to select the alternate shruti ...
final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
userShrutiLabel,
closestShruti.getLabel());
this.setMessage(msg);
this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
mActivity.getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(mOnClickListerner != null) {
mOnClickListerner.proceedWithChange(closestShruti);
}
}
});
this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
mActivity.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
public interface OnClickedListener {
void proceedWithChange(final Shruti closestShruti);
}
}
我不确定为什么它无法获取默认模板并显示它。当我使用 AlerDialog.Builder(Context)
,而没有创建自定义 class,我能够正确地看到对话框。有什么建议吗?
非常非常老的问题:-)
自定义警报对话框 - How to create a Custom Dialog box in android?
自定义对话框主题 - How to change theme for AlertDialog
**--java file code----**
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
**----and you can put into xml file
styles.xml - Custom style----**
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>
**---and also you can add new style----**
<style name="MyTitleTextStyle">
<item name="android:textColor">#FFEB3B</item>
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>
**---and alert dialong style---**
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
...
<item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>
我找到了解决方案。基本上,我不应该为 AlertDialog
覆盖 onCreate()
方法。如果我这样做,那么通常会提供用于渲染的布局。相反,无论我在上面代码的 onCreate()
方法中做了什么,我都将它移到另一个函数中,然后从 class 的构造中调用它,这对我有用。代码类似于:
public class ClosestShrutiDialog extends AlertDialog {
private String mLessonId;
private Activity mActivity;
private View mParentView;
private OnClickedListener mOnClickListerner;
public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
super(activity);
this.mLessonId = lessonId;
this.mActivity = activity;
this.mParentView = parentView;
setupView();
}
public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
this.mOnClickListerner = mOnClickListerner;
}
private void setupView() {
// My business login to decide the msg related params ....
// showing message to user to select the alternate shruti ...
final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
userShrutiLabel,
closestShruti.getLabel());
this.setMessage(msg);
this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
mActivity.getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(mOnClickListerner != null) {
mOnClickListerner.proceedWithChange(closestShruti);
}
}
});
this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
mActivity.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
public interface OnClickedListener {
void proceedWithChange(final Shruti closestShruti);
}
}
我正在尝试使用默认主题构建自定义 AlertDialog
,以便我可以在代码中的多个位置使用相同的 AlertDialog
。我通过扩展 AlertDialog
并编写我的自定义业务逻辑来决定对话框中的消息来创建 class。问题是,当我调用 AlertDialog.show()
时,我看到一个没有显示对话框的黑色覆盖层。这是我的代码:
public class ClosestShrutiDialog extends AlertDialog {
private String mLessonId;
private Activity mActivity;
private View mParentView;
private OnClickedListener mOnClickListerner;
public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
super(activity);
this.mLessonId = lessonId;
this.mActivity = activity;
this.mParentView = parentView;
}
public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
this.mOnClickListerner = mOnClickListerner;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// My business login to decide the msg related params ....
// showing message to user to select the alternate shruti ...
final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
userShrutiLabel,
closestShruti.getLabel());
this.setMessage(msg);
this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
mActivity.getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(mOnClickListerner != null) {
mOnClickListerner.proceedWithChange(closestShruti);
}
}
});
this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
mActivity.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
public interface OnClickedListener {
void proceedWithChange(final Shruti closestShruti);
}
}
我不确定为什么它无法获取默认模板并显示它。当我使用 AlerDialog.Builder(Context)
,而没有创建自定义 class,我能够正确地看到对话框。有什么建议吗?
非常非常老的问题:-)
自定义警报对话框 - How to create a Custom Dialog box in android?
自定义对话框主题 - How to change theme for AlertDialog
**--java file code----**
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
**----and you can put into xml file
styles.xml - Custom style----**
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>
**---and also you can add new style----**
<style name="MyTitleTextStyle">
<item name="android:textColor">#FFEB3B</item>
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>
**---and alert dialong style---**
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
...
<item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>
我找到了解决方案。基本上,我不应该为 AlertDialog
覆盖 onCreate()
方法。如果我这样做,那么通常会提供用于渲染的布局。相反,无论我在上面代码的 onCreate()
方法中做了什么,我都将它移到另一个函数中,然后从 class 的构造中调用它,这对我有用。代码类似于:
public class ClosestShrutiDialog extends AlertDialog {
private String mLessonId;
private Activity mActivity;
private View mParentView;
private OnClickedListener mOnClickListerner;
public ClosestShrutiDialog(@NonNull Activity activity, final String lessonId, final View parentView) {
super(activity);
this.mLessonId = lessonId;
this.mActivity = activity;
this.mParentView = parentView;
setupView();
}
public void setmOnClickListerner(OnClickedListener mOnClickListerner) {
this.mOnClickListerner = mOnClickListerner;
}
private void setupView() {
// My business login to decide the msg related params ....
// showing message to user to select the alternate shruti ...
final String msg = mActivity.getResources().getString(R.string.user_alternate_shruti_msg,
userShrutiLabel,
closestShruti.getLabel());
this.setMessage(msg);
this.setButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE,
mActivity.getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(mOnClickListerner != null) {
mOnClickListerner.proceedWithChange(closestShruti);
}
}
});
this.setButton(android.support.v7.app.AlertDialog.BUTTON_NEGATIVE,
mActivity.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
public interface OnClickedListener {
void proceedWithChange(final Shruti closestShruti);
}
}