2016-07-27 57 views
6

我有一個彈出窗口,可以在我的應用程序中下載音頻指令。我想要做的是我想要將默認文字顏色「OK」改爲藍色。我嘗試了一些東西,但沒有奏效。這是我的代碼:更改警報對話框的文本顏色

private void showDownloadPgmPopup() { 

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity()); 
    builder.setTitle("Download instructional audio?"); 
    builder.setMessage(ParamConstants.AUDIODOWNLOADPERMISSION); 
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel); 
      goToMoveScreen(); 
     } 
    }); 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      AndroidDownloadFileByProgressBarActivity.StartAudioAssetDownload(getActivity()); 

     } 

    }).create(); 
    // change the text color of download instruction ok button 


    final android.app.AlertDialog dialog = builder.show(); 
    dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
            @Override 
            public void onShow(DialogInterface arg0) { 
             dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#ff5722")); 
            } 
           }); 
    dialog.setCanceledOnTouchOutside(false); 
    dialog.show(); 
} 

但這種變化並不能反映任何人都可以告訴我我做錯了什麼?

+0

[如何在android中創建自定義對話框?](http:// st ackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android) – SaravInfern

回答

2

你有兩種方法可以做到這

  1. 覆蓋默認對話框。
//1. create a dialog object 'dialog' 
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Exit", errorMessage); 
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        ... 
       } 

      }).create(); 
//2. now setup to change color of the button 
dialog.setOnShowListener(new OnShowListener() { 
    @Override 
    public void onShow(DialogInterface arg0) { 
     dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#f34235")); 
    } 
} 

dialog.show() 
  • 創建自己的custom對話框
  • // create instance of dialog 
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    
    // get inflater and inflate layour for dialogue 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.alert_label_editor, null); 
    
    // now set layout to dialog 
    dialogBuilder.setView(dialogView); 
    
    // create instance like this OR directly mentioned in layout 
    Button button= (Button) dialogView.findViewById(R.id.label_field); 
    button.setText("test label"); 
    AlertDialog alertDialog = dialogBuilder.create(); 
    
    // show dialog 
    alertDialog.show(); 
    
    5

    警告對話框完全定製here

    樣品:

    enter image description here

    6

    你必須提供在AlertDialog構造一個自定義樣式ID:

    AlertDialog.Builder(Context context, int themeResId) 
    

    和樣式文件應該是這樣的:

    <style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert"> 
        <item name="android:colorAccent">#0000FF</item> 
    </style>