2012-01-14 29 views
5

Có thể tùy chỉnh các nút tích cực và tiêu cực trong AlertDialog không? Tôi cần thay thế giao diện mặc định là dương và âm với tùy chỉnh.Có thể tùy chỉnh các nút tích cực và tiêu cực trong AlertDialog không?

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {... 
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {... 

Ai đó có thể cho tôi biết cách thực hiện điều đó?

+3

Bạn sẽ phải tạo một hộp thoại tùy chỉnh. :/Đây là hướng dẫn đơn giản nhất tôi có thể tìm thấy. http://i.thiyagaraaj.com/articles/android-articles/customdialogboxpopupusinglayoutinandroid – JustinDanielson

Trả lời

1

nếu bạn muốn thay đổi họ, tôi sẽ đề nghị sử dụng hoạt động với cách bố trí theo yêu cầu của bạn và thêm chúng = Dialog trong hoạt động của bạn, trong tập tin Manifest

1

nếu bạn muốn tùy chỉnh, bạn có thể sử dụng hộp thoại thay vì cảnh báo thoại đây là mẫu mã

final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar); 
    View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null); 
    view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img); 
    img.setImageBitmap(issue.getCoverImage()); 

    dialog.setContentView(view); 
    dialog.show(); 

bạn có thể thiết lập cách bố trí trong hộp thoại và thêm nhấp chuột listner vào nó

1

Yuo có thể thiết lập mỗi quan điểm trong hộp thoại. Bạn có thể đặt chế độ xem bằng hai nút và không đặt các nút tiêu cực là &.

dụ:

AlertDialog.Builder builder = 
      new AlertDialog.Builder(this); 

View dialogView = LayoutInflater.from(this) 
      .inflate(R.layout.my_layout, null); 

builder.setView(dialogView); 
+0

ngoài ngữ cảnh! –

+0

@Jivraj S Shekhawat Ok, bất cứ điều gì. Nếu bạn có thể cung cấp câu trả lời tốt hơn, hãy làm điều đó –

5
public class ComentarDialog extends DialogFragment{ 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    builder.setMessage("Mensaje de alerta") 
      .setTitle("Comentar") 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }) 
      .setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }); 

    return builder.create(); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    //Personalizamos 

    Resources res = getResources(); 

    //Buttons 
    Button positive_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE); 
    positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog)); 

    Button negative_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE); 
    negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog)); 

    int color = Color.parseColor("#304f5a"); 

    //Title 
    int titleId = res.getIdentifier("alertTitle", "id", "android"); 
    View title = getDialog().findViewById(titleId); 
    if (title != null) { 
     ((TextView) title).setTextColor(color); 
    } 

    //Title divider 
    int titleDividerId = res.getIdentifier("titleDivider", "id", "android"); 
    View titleDivider = getDialog().findViewById(titleDividerId); 
    if (titleDivider != null) { 
     titleDivider.setBackgroundColor(color); 
    } 
} 
} 
+0

Câu trả lời hoàn hảo. cám ơn vì đã chia sẻ. :) –

+0

FYI 'button.setBackground()' ... yêu cầu cấp API 16 – estoke

Các vấn đề liên quan