use*_*538 11 android android-alertdialog android-dialog
这是我的代码.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vi = inflater.inflate(R.layout.group_dialog_layout,null);
builder.setView(vi);
TextView txtNewGroupEntry = (TextView) vi.findViewById(R.id.txtGroupRename);
if(isNew==true){
builder.setTitle("New Group");
txtNewGroupEntry.setText(R.string.new_group_instruction);
}
builder.setPositiveButton(R.string.ok_button, null);
builder.setNegativeButton(R.string.cancel_button, null);
AlertDialog dialog = builder.create();
dialog.show();
Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Run Code Online (Sandbox Code Playgroud)
我有一个带有添加按钮和取消按钮的警告对话框.我希望按钮的文本都是粗体和斜体.我该怎么做?
Gop*_*opi 16
这个示例代码将帮助您......
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message").setTitle("title");
AlertDialog alertDialog = builder.create();
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// do work
break;
case DialogInterface.BUTTON_NEUTRAL:
// do work
break;
case DialogInterface.BUTTON_NEGATIVE:
// do work
break;
default:
break;
}
}
};
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", listener);
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", listener);
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Cancel",
listener);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
AlertDialog alertDialog = (AlertDialog) dialog;
Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);
button = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);
button = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);
}
});
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 14
而不是将按钮文本设置为"add",将其设置为Html.fromHtml("<b><i>add</i></b>")
所以对于你的代码:
改变这些行:
builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);
Run Code Online (Sandbox Code Playgroud)
对于这些线:
builder.setPositiveButton(Html.fromHtml("<b><i>" + getString(R.string.ok_button) + "</i><b>"), null);
// and
builder.setNegativeButton(Html.fromHtml("<b><i>" + getString(R.string.cancel_button) + "</i><b>"), null);
Run Code Online (Sandbox Code Playgroud)
要么
你可以修改strings.xml文件中的字符串.
例如,如果您的字符串看起来像这样:
<string name="ok_button">add</string>
<string name="cancel_button">cancel</string>
Run Code Online (Sandbox Code Playgroud)
你可以改成它们:
<string name="ok_button"><b><i>add</i></b></string>
<string name="cancel_button"><b><i>cancel</i></b></string>
Run Code Online (Sandbox Code Playgroud)
但
你仍然错误地引用你的字符串资源.而不是R.string.ok_button,因为它返回一个int,你将不得不使用getString(R.string.ok_button)
所以你必须改变这些行:
builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);
Run Code Online (Sandbox Code Playgroud)
对于这些线:
builder.setPositiveButton(getString(R.string.ok_button), null);
// and
builder.setNegativeButton(getString(R.string.cancel_button), null);
Run Code Online (Sandbox Code Playgroud)
您可以像我在下面提到的那样访问您的Dialog's buttons内部onShow().在onShow()触发时你可以访问yoir对话框的UI,试试这个,
private void showAlertDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Dailog Demo!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alertdialog = builder.create();
alertdialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
((Button)alertdialog.getButton(Dialog.BUTTON_POSITIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
((Button)alertdialog.getButton(Dialog.BUTTON_NEGATIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
}
});
alertdialog.show();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10006 次 |
| 最近记录: |