我打算用layout_weight = 1创建3个按钮,对自定义对话框不感兴趣.所以我写了下面的代码.它不工作.总是是按钮给我null.这段代码怎么了?
AlertDialog dialog= new AlertDialog.Builder(this).create();
dialog.setIcon(R.drawable.alert_icon);
dialog.setTitle("title");
dialog.setMessage("Message");
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Log.w("Button",""+yesButton);//here getting null
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
yesButton.setLayoutParams(layoutParams);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
此致,Android开发人员.
我想AlertDialog用3个按钮写一个.如果不满足某个条件,我希望禁用中间的中性按钮.
码
int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("You have Encountered a Monster");
alertbox.setPositiveButton("Fight!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
createMonster();
fight();
}
});
alertbox.setNeutralButton("Try to Outwit",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
// This should not be static
// createTrivia();
trivia();
}
});
// Return to …Run Code Online (Sandbox Code Playgroud) 我想只在用户输入不为空时才启用alertDialog肯定按钮,如果用户输入为空则禁用它,为了验证目的切换为王,用户输入不能为空.这是我的代码,即使我把一个字符串按钮没有激活.
buildInfos.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
infosDesc = inputInfos.getText().toString();
Log.i("DESC", infosDesc);
drawBetween2LastPoints(getAlertColor(alertType), "title", infosDesc);
}
});
AlertDialog buildInfosDialog = buildInfos.create();
buildInfosDialog.show();
if(infosDesc.isEmpty()){
buildInfosDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}
Run Code Online (Sandbox Code Playgroud)