繁体   English   中英

AlertDialog的“确定”按钮中的倒计时

[英]Countdown in OK button of an AlertDialog

我已经在我的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秒倒计时集成到“确定”按钮中。 在此倒计时过程中,不应通过单击“确定”按钮关闭AlertDialog(应在倒计时期间将其禁用)。

像下面这样写你的代码

// 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM