打开弹出窗口

时间:2017-12-19 16:52:35

标签: android popup conditional-statements

我有一个弹出窗口每隔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
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

使用您的代码,对话框将在两次中打开一次,因为每次增加totalCount并在检查totalCount模2后

首先打开totalCount = 1(打开对话框)

第二次打开totalCount = 2(对话框仍然关闭)

第三次打开totalCount = 3(打开对话框)