动态应用程序按钮

Dynamic app button

我想创建一个类似于 WhatsApp 中的按钮,用于使用麦克风录制语音。

基本上,如果用户开始输入内容,则该麦克风按钮会转换为发送文本按钮。但是,如果用户删除文本,则该按钮会再次变为麦克风按钮。谁能告诉我如何创建这样的按钮?

使用 TextWatcher 来检测 EditText 的内容何时被更改并执行您想要的操作。

示例:

yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        //called after the EditText's text is changed

        if (editable.length() > 0) {
            //change to send message icon
        } else {
            //change to microphone icon
        }
    }
});

对于您的按钮 onClickListener,只需检查 EditText 是否为空。

示例:

yourButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        String text = yourEditText.getText().toString();
        if (text.isEmpty()) {
            //perform your microphone action
        } else {
            //perform your send message action
        }
    }
});