简体   繁体   中英

AlertDialogPro with custom Typeface

I'm trying to set a custom typeface to a dialog built using AlertDialogPro but get a NPE when trying to retrieve the dialog Button using dialog.getButton(int) . And how can I set the typeface for the message and title of the dialog?

AlertDialogPro.Builder builder = new AlertDialogPro.Builder(this);
   builder.setMessage(getResources().getString(R.string.dialog_body))
   .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
          // Do stuff
      }
});

AlertDialogPro dialog = builder.create();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTypeface(customTf));
dialog.show();

Log:

03-02 16:30:22.982: E/AndroidRuntime(15596): 
java.lang.NullPointerException: Attempt to invoke virtual method 'void android
.widget.Button.setTypeface(android.graphics.Typeface)' on a null
object reference

您只能在调用dialog.show()之后访问对话框按钮(通过dialog.getButton(DialogInterface.BUTTON_POSITIVE) dialog.show()

Call this method:

builder.setPositiveButton(int textId,final OnClickListener listener)

before creating the dialog

Fixed it by doing two things:

  1. Moved the getButton() to after dialog.show()
  2. Used findViewById() to retrieve the message textview

Code:

AlertDialogPro.Builder builder = new AlertDialogPro.Builder(this);
   builder.setMessage(getResources().getString(R.string.dialog_body))
   .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
          // Do stuff
      }
});

AlertDialogPro dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTypeface(customTf));
((TextView) dialog.findViewById(R.id.adp_message)).setTypeface(customTf);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM