我对 onClick 监听器有一个小问题。我有一个带有正面和负面按钮的对话框,正面按钮应该像这样工作:
if( statement == true ) -> 点击肯定按钮会做一些事情, if( statement == false ) -> 点击肯定按钮会显示 Toast.makeText() 但这不会关闭对话框窗口。
提前致谢!
您需要调用 close 方法才能关闭它。将此行放在 pos/neg 按钮侦听器的最后一行:
dialog.cancel();
其中 dialog 是作为参数传递的 DialogInterface 的实例。在
DialogInterface.OnClickListener.onClick(DialogInterface ,int);
更新:
builder.setNeutralButton( "Ustaw", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
getRecurrence( whichType, finishType );
if( isRecurrenceProperlySet ) {
//do some stuff
}
else {
Toast.makeText( contextThemeWrapper, "Set data!", Toast.LENGTH_SHORT ).show();
}
dismiss();
}
});
正如我之前所说,将该行移至侦听器的最后一行。
一旦尝试这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
findViewById(R.id.test).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
show();
}
});
}
AlertDialog alertDialogBox;
private OnClickListener clickListner = new OnClickListener() {
@Override
public void onClick(View v) {
show();
}
};
private boolean conditionVal = false;
public void show() {
AlertDialog.Builder b = new AlertDialog.Builder(this);
alertButtonValue = false;
b.setNegativeButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
b.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
this.alertDialogBox = b.create();
this.alertDialogBox.setTitle("Helllo");
this.alertDialogBox.setMessage("message...");
alertDialogBox.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if (alertButtonValue == false) {
Button button = alertDialogBox
.getButton(DialogInterface.BUTTON_NEGATIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!conditionVal)
Toast.makeText(getApplicationContext(),
"Condition not satisfied",
Toast.LENGTH_SHORT).show();
}
});
alertButtonValue = true;
} else {
Toast.makeText(getApplicationContext(), "Hello",
Toast.LENGTH_SHORT).show();
}
}
});
this.alertDialogBox.show();
}
private boolean alertButtonValue = false;