AlertDialog的OK按钮倒计时

时间:2015-03-29 20:01:12

标签: java android alertdialog countdown

我已将此对话框实施到我的Android应用程序中,并且工作正常。 How to make a "do not ask me again" dialog pop-up box? Android

这是我的onResume()方法:

@Override
protected void onResume() 
{
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Info");
    adb.setMessage(Html.fromHtml("Readme"));

    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int which) 
            {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                editor.commit();
                return;
            }
        });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");

    if (!skipMessage.equals("checked"))
        adb.show();

    super.onResume();
}

但是如何将10秒倒计时器集成到OK按钮中。在此倒计时期间,单击“确定”按钮不应关闭AlertDialog(在倒计时时应禁用它)。

1 个答案:

答案 0 :(得分:0)

编写如下代码

// Create a handler
Handler handler = new Handler();
AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Info");
    adb.setMessage(Html.fromHtml("Readme"));

    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int which) 
            {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                editor.commit();
                return;
            }
        });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");

    if (!skipMessage.equals("checked"))
        AlertDialog dialog = adb.create();
        dialog.show();

    final Button button;
    button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    if(dialog.isShowing()){

        button.setClickable(false);
    }

    // Post the task to set it clickable in 10000ms
    handler.postDelayed(new Runnable(){
        @Override
        public void run() {
            //if(null !== button || button !== null)
                button.setClickable(true);
        }}, 10000);