如何在方法运行期间停止用户交互?

时间:2019-02-04 15:10:50

标签: java android android-studio

我有一堆按钮(键盘),它们使用一些使这些按钮具有动画效果的代码来调用方法。如果用户按下按钮的速度过快,则会使动画混乱。因此,我需要在该方法期间禁用用户交互。我试图在方法的开始和结束处设置窗口标志,甚至禁用按钮。但是还是。如果用户按下按钮的速度过快,就好像该应用没有时间设置标志。

public void onClickKeyboard (View view) {


    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);



    if (myAnswers[gameState].charAt(s.length())== ' ') {
          s = s + " ";
    }
        s = s + view.getTag().toString();

        if (s.equalsIgnoreCase(myAnswers[gameState].substring(0,s.length()))){

            ViewCompat.animate(view).alpha(0).setDuration(200).start();

            //  view.setVisibility(View.INVISIBLE);

                preguntaViewInput = myQuestions[gameState] + s.substring(0,1).toUpperCase()+s.substring(1).toLowerCase();

            preguntaView.setText(preguntaViewInput, TextView.BufferType.SPANNABLE);
            numberKeysPressed++;

        } else {

            s = s.substring(0, s.length() - 1); // sense aixo lapp crash quan sapreten 10 lletres incorrectes seguides
            b = (Button) view;

            view.setBackgroundResource(R.drawable.button_keyboard_wrong);
            if (answerFor.substring(numberKeysPressed, answerFor.length()).contains(((Button) view).getText().toString())){
                ViewCompat.animate(view).rotationBy(360).scaleY((float) 0.7).scaleX((float) 0.7).setDuration(200).withEndAction(new Runnable() {
                    @Override
                    public void run() {

                        ViewCompat.animate(b).rotationBy(360).scaleY((float) 1).scaleX((float) 1).setDuration(200).withEndAction(new Runnable() {
                            @Override
                            public void run() {

                                b.setBackgroundResource(R.drawable.buttonpurchase);

                            }
                        });

                    }
                });

            } else {

                ViewCompat.animate(view).rotationBy(360).scaleY((float) 0.7).scaleX((float) 0.7).setDuration(200).start();
            }
        }



    if (s.toString().equalsIgnoreCase(myAnswers[gameState])) {
        Spannable ss = (Spannable) preguntaView.getText();

        ss.setSpan(new ForegroundColorSpan(Color.GREEN), myQuestions[gameState].length(), s.length() + myQuestions[gameState].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        buttonNext.setBackgroundColor(Color.argb(64, 0, 250, 0));
        String nextString = "correct";
        buttonNext.setText(nextString);
        buttonNext.setAlpha(0);
        buttonNext.setVisibility(View.VISIBLE);
        ViewCompat.animate(buttonNext).alpha(1).setDuration(300).start();
    }



    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

}

0 个答案:

没有答案