我有一个弹出窗口每隔x次显示在屏幕上但是我希望在用户按下Send Button
之后我希望弹出窗口在x开口后停止出现这是弹出窗口的代码:
private void dialog() {
myDialog.setContentView(R.layout.activity_pop_up);
editTextEmailValue = (EditText) myDialog.findViewById(R.id.editTextEmail);
editTextMessageValue = (EditText) myDialog.findViewById(R.id.editTextMessage);
editTextNumeValue = (EditText) myDialog.findViewById(R.id.editTextNume);
Button btnSend = (Button) myDialog.findViewById(R.id.sendBtn);
Button btnClose = (Button) myDialog.findViewById(R.id.close);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check() == 0) {
sendMail();
myDialog.dismiss();
}
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.setCancelable(false);
myDialog.show();
}
以下是我计算应用开放的方式:
//this in in onCreate method
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
if (totalCount % 2 == 0) {
dialog();
}
我试图在 if(totalCount % 2 == 0 && stop == 1)
中设置一个条件,我把 stop = 0
放在 btnSend.onClickListener()
中,如下所示:
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check() == 0) {
sendMail();
myDialog.dismiss();
stop = 0; //Note that the stop variable is public
}
}
});
使用您的代码,对话框将在两次中打开一次,因为每次增加totalCount并在检查totalCount模2之后
首次打开totalCount = 1(对话框打开)
第二次打开totalCount = 2(对话框仍然关闭)
第三次打开totalCount = 3(对话框打开)