启用和禁用按钮一定时间

时间:2018-07-09 11:37:25

标签: android firebase firebase-realtime-database

我是Android Studio的新手。我想做一个聊天应用。 因此,我想在他/她发送消息后稍加延迟。 例如,我写输入“ Hello”,如果再次发送,它将显示Toast以显示时间,分别是5秒,4秒直到完成。然后,我将能够再次发送邮件。

而且,如果我在这段时间内按下按钮,它将显示能够发送消息的秒数。

我制造了一个,但没有按预期运行。

private void buttonFabSend() {
        floatingActionButtonSendText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CountDownTimer( 5000, 1000){
                    @Override
                    public void onTick(long millisUntilFinished) {
                        Toast.makeText(getApplicationContext(),millisUntilFinished/1000 +"seconds",Toast.LENGTH_SHORT ).show();
                        floatingActionButtonSendText.setEnabled(false);
                    }
                    @Override
                    public void onFinish() {
                        editTextInput = findViewById(R.id.editTextChat);
                        if (TextUtils.isEmpty(editTextInput.getText().toString())) {
                            editTextInput.setError("Enter your message");
                            editTextInput.requestFocus();
                            return;
                        }
                        floatingActionButtonSendText.setEnabled(true);
                        FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(new chatMessage(editTextInput.getText().toString(), FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), String.valueOf(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl())));
                        editTextInput.setText("");
                    }
                }.start();
            }
        });
    }

3 个答案:

答案 0 :(得分:2)

取消注释您的发送代码

    int _count = 5;//5 seconds
    private boolean canSendMessage = true;
    private Runnable countDown = new Runnable() {
        @Override
        public void run() {
            while (_count > 0) {
                _count--;
                try {
                    Thread.sleep(1000);//1 second
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            _count = 5;//again set to 5 seconds
            canSendMessage = true;//enable send

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_test);

        final Button send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (canSendMessage) {
                    //
//                    editTextInput = findViewById(R.id.editTextChat);
//                    if (TextUtils.isEmpty(editTextInput.getText().toString())) {
//                        editTextInput.setError("Enter your message");
//                        editTextInput.requestFocus();
//                        return;
//                    }
//                    floatingActionButtonSendText.setEnabled(true);
//                    FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(new chatMessage(editTextInput.getText().toString(), FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), String.valueOf(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl())));
//                    editTextInput.setText("");
                    canSendMessage = false;
                    Thread t = new Thread(countDown);
                    t.start();
                } else {
                    Toast.makeText(UploadTest.this, "You can send After " + _count + " Seconds", Toast.LENGTH_SHORT).show();
                }

            }
        });


    }

答案 1 :(得分:1)

您可以使用以下逻辑:

private long delay = 4000;

void onViewClick(View view) {
    view.setEnabled(false);
    view.postDelayed(() -> {
        view.setEnabled(true);
    }, delay);
}

您最好检查延迟后视图是否仍附加到窗口上

private long delay = 4000;
void onViewClick(View view) {
    view.setEnabled(false);
    view.postDelayed(() -> {
        if (ViewCompat.isAttachedToWindow(view) {
            view.setEnabled(true);
        }
    }, delay);
}

答案 2 :(得分:0)

在您的代码中,每次新的倒数计时器都会初始化,因此它将无法正常工作,您需要做的只是将倒数计时器启动一次直到结束。请尝试以下代码

boolean isRunning = false;
long currentMillisUntilFinished = 0;

并用以下代码替换 floatingActionButtonSendText click事件

floatingActionButtonSendText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (isRunning) {
            Toast.makeText(getApplicationContext(), "seconds remaining: " + currentMillisUntilFinished / 1000, Toast.LENGTH_SHORT).show();
            return;
        }

        new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
                currentMillisUntilFinished = millisUntilFinished;
                isRunning = true;

            }

            public void onFinish() {
                isRunning = false;
                editTextInput = findViewById(R.id.editTextChat);
                if (TextUtils.isEmpty(editTextInput.getText().toString())) {
                    editTextInput.setError("Enter your message");
                    editTextInput.requestFocus();
                    return;
                }
                FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(new chatMessage(editTextInput.getText().toString(), FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), String.valueOf(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl())));
                editTextInput.setText("");
            }

        }.start();
    }
});