Как задержать строку кода после нажатия кнопки - PullRequest
0 голосов
/ 14 апреля 2020

Я новичок в Java и пытаюсь отложить одну строку кода после того, как пользователь нажмет кнопку. Строка кода содержит массив, i ++ . Причина, по которой я хотел бы это сделать, заключается в том, что я анимирую два текстовых представления путем их исчезновения. Один textView исчезает, в то время как другой исчезает, и я пытаюсь изменить отображаемое слово, только увеличивая индекс моего массива после того, как у них есть та же самая Альфа, которая составляет половину установленной длительности анимации, 500. Я посмотрел онлайн, и у меня есть видел, как некоторые предлагали таймер качания , а другие предлагали использовать thread.sleep . Какие-либо предложения? Вот код этой кнопки:).

public void nextWord(View view) { //nextWord is the onclick of the button


        Button nextButton = findViewById(R.id.nextButton);
        Button showTextButton = findViewById(R.id.showTextButton);
        TextView wordTextView = findViewById(R.id.wordTextView);
        EditText editTextView = findViewById(R.id.enterEditText);
        ImageView logoImageView = findViewById(R.id.logoImageView);
        TextView wordTextView1 = findViewById(R.id.wordTextView1);


        i++; // need to delay this


        String displayHint;
        String displayText;

        displayText = enterWord() + chooseArray();
        displayHint = chooseArray();

        wordTextView.setText(displayText);
        editTextView.setHint(displayHint);
        wordTextView1.setText(displayText) ;



        enteredWords[i] = editTextView.getText().toString();

        if (i%2 == 0) {
            ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(wordTextView, View.ALPHA, 1f, 0f);
            alphaAnimation.setDuration(1000);
            ObjectAnimator otherAlphaAnimation = ObjectAnimator.ofFloat(wordTextView1, View.ALPHA, 0f, 1f);
            otherAlphaAnimation.setDuration(1000);

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(otherAlphaAnimation,alphaAnimation);
            animatorSet.start();

        } else {
            ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(wordTextView, View.ALPHA, 0f, 1f);
            alphaAnimation.setDuration(1000);
            ObjectAnimator otherAlphaAnimation = ObjectAnimator.ofFloat(wordTextView1, View.ALPHA, 1f, 0f);
            otherAlphaAnimation.setDuration(1000);

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(otherAlphaAnimation,alphaAnimation);
            animatorSet.start();
        }


    }

1 Ответ

0 голосов
/ 14 апреля 2020

Попробуйте с

new Handler().postDelayed(Runnable r, long delay)

Используйте это как

new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // fade in new text view
                        }
                    }, 1000);   // time needed for first animation to finish

Теперь, когда первая анимация занимает 1000 мс до финала sh, вторая будет запускаться и после 1000 мс. Вы можете поместить его в значение final, чтобы манипулировать этими задержками и синхронизировать их

...