我正在尝试使用默认主题构建自定义AlertDialog
,以便我可以在代码中的多个位置使用相同的AlertDialog
。我通过扩展AlertDialog
并编写自定义业务逻辑来创建一个类来决定对话框中的消息。问题是,当我拨打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)
而不创建自定义类时,我能够正确地看到对话框。有什么建议吗?
答案 0 :(得分:0)
非常古老的问题: - )
自定义提醒对话框 - How to create a Custom Dialog box in android?
自定义对话框主题 - How to change theme for AlertDialog
答案 1 :(得分:0)
**--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>