如何在编辑文本不为空的情况下更改按钮?

huangapple go评论61阅读模式
英文:

How to change button if edit text is not empty?

问题

我创建了一个定时器,用户有45秒的时间来输入短信。如果用户开始输入,按钮应更改为可用,并更改文本为“验证短信”。
如果时间到期,它应该提供重新发送短信的选项。因此,按钮应更改为可用,并且文本应更改为“再次发送”,就像上次一样,但是针对不同的功能。

我如何才能使这样做,以便在按钮的两种状态之间不会发生冲突(验证和再次发送)?目前我的代码是这样工作的,如果用户开始输入并点击按钮,计时器会重置,这是不应该发生的。当用户输入时,按钮应该是可用的,但是当用户点击短信时,应该验证短信,而不是执行其他功能。

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

    t1 = (TextView)findViewById(R.id.t1);
    b1 = (Button) findViewById(R.id.button1);
    sms = (EditText) findViewById(R.id.EditTextSMS);

    mCountDownTimer = new CountDownTimer(45000, 1000) {
        public void onTick(long millisUntilFinished) {
            sms.addTextChangedListener(loginTextWatcher);
            t1.setText("00:" + millisUntilFinished / 1000);
            if(millisUntilFinished <= 10000){
                t1.setText("00:0" + millisUntilFinished / 1000);
            }
        }

        public void onFinish() {
            t1.setText("Time is over");
            b1.setEnabled(true);
        }
    }.start();

    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mCountDownTimer.start();
        }
    });
}

private TextWatcher loginTextWatcher = 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) {
        String smsInput = sms.getText().toString().trim();
        b1.setEnabled(!smsInput.isEmpty() );
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }
};
英文:

I did a timer that the user has 45 seconds to type the SMS.If he starts typing the button should change to enable and change text to "Validate SMS".
If the time has expired it should give the option to send the SMS again.So the button should change to enabled and text should change to "Send again" like the other time but for a different function.

How can i make this so that i don't have a conflict between the two states of a button(Validade and send again)?.The way my code is working right now is that if the user starts typing and hit the button the timer resets and that should never happen.When the user types the button should be enabled but when the user clicks the sms should be validate and not go for the other function.

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
t1 = (TextView)findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
sms = (EditText) findViewById(R.id.EditTextSMS);
mCountDownTimer = new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
sms.addTextChangedListener(loginTextWatcher);
t1.setText(&quot;00:&quot; + millisUntilFinished / 1000);
if(millisUntilFinished&lt;=10000){
t1.setText(&quot;00:0&quot; + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText(&quot;Time is over&quot;);
b1.setEnabled(true);
}
}.start();
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCountDownTimer.start();
}
});
}
private TextWatcher loginTextWatcher = 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) {
String smsInput = sms.getText().toString().trim();
b1.setEnabled(!smsInput.isEmpty() );
}
@Override
public void afterTextChanged(Editable editable) {
}
};

答案1

得分: 0

你可以创建一个名为“重试”的新按钮。当“重试”状态发生时,你可以将可见性状态设为可见。
第一种情况当然必须是“隐藏”,并且你必须确保将它定位在正确的坐标上。
最后,你可以继续处理你的监听器。

英文:

You can create a new button named "Try Again". When the "Retry" status happens, you can make the visibilty state visible.
The first situation must of course be "gone" and you have to be careful that you position it in the correct coordinates.
Finally, you can continue with your listener.

huangapple
  • 本文由 发表于 2020年10月1日 21:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64156375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定