Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使按钮在一定时间内处于启用和禁用状态_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Android 使按钮在一定时间内处于启用和禁用状态

Android 使按钮在一定时间内处于启用和禁用状态,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我是安卓工作室的新手。我想做一个聊天应用程序。 因此,我想在他/她发送消息后进行延迟。 例如,我写输入Hello,如果我再次发送,它将显示Toast以显示时间,5秒,4秒直到完成。然后我将能够再次发送消息 而且,如果我在这段时间内按下按钮,它将显示能够发送消息的秒数 我做了一个,但没有按预期运行 private void buttonFabSend() { floatingActionButtonSendText.setOnClickListener(new View.OnCli

我是安卓工作室的新手。我想做一个聊天应用程序。 因此,我想在他/她发送消息后进行延迟。 例如,我写输入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();
            }
        });
    }

取消对发送消息的代码的注释

    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();
                }

            }
        });


    }

在您的代码中,每次新的倒计时计时器将初始化,所以它将无法正常工作,您需要做的只是启动倒计时计时器一次,直到它完成。请尝试下面的代码

boolean isRunning = false;
long currentMillisUntilFinished = 0;
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();
    }
});
并将floatingActionButtonSendText click事件替换为以下代码

boolean isRunning = false;
long currentMillisUntilFinished = 0;
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();
    }
});

您可以使用如下逻辑:

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

@Zuhrain,我已经更新了我的答案,用我的替换你的浮动操作按钮SendText点击事件代码。