[英]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.