[英]How to set color or highlight PositiveButton and NegativeButton in alert dialog box and customized
我正在警報對話框正按鈕中搜索設置顏色。 但是我沒有得到任何解決方案,所以請建議我為警報對話框提供任何一個好的解決方案。
我的代碼在這里
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Delete ");
alertDialogBuilder
.setMessage("Are you sure, you want to delete ")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
datab=mdb.getWritableDatabase();
datab.execSQL("DELETE FROM demo WHERE student_id='"+getid+"'");
Toast.makeText(getApplicationContext(), "Successfully deleted", 10).show();
Intent i=new Intent(StudentInfo.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i); }
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
我想在警報對話框中為 Yes Button 設置紅色。
就一個簡單的! 在 Java 類中的任何位置創建一個類似這樣的對話框方法。
public void openDialog() {
final Dialog dialog = new Dialog(context); // context, this etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
現在創建布局 XML dialog_demo.xml 並創建您的 UI/設計。 這是我為演示目的創建的示例。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/dialog_info">
<Button
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="@color/dialog_cancel_bgcolor"
android:text="Cancel"/>
<Button
android:id="@+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="@color/dialog_ok_bgcolor"
android:text="Agree"/>
</LinearLayout>
</RelativeLayout>
現在你可以從任何你喜歡的地方調用 openDialog() :) 這是上面代碼的截圖
請注意,strings.xml 和colors.xml 中使用了文本和顏色。 你可以定義你自己的。 希望這是有幫助的。 謝謝!
其他明智的你可以使用這個代碼它對我有用
public void createDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit from app");
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app",
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackgroundColor(Color.MAGENTA);
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackgroundColor(Color.YELLOW);
}
您可以通過以下方式獲取按鈕:
alertdialog.show();
//possible Buttons: BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_NEUTRAL
Button myPositiveButton = alertdialog.getButton(AlertDialog.BUTTON_POSITIVE);
//pseudo code for now:
myPositiveButton.setOnClickListener(...);
myPositiveButton.setBackgroundColor(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.