【发布时间】:2019-08-08 11:32:22
【问题描述】:
我想针对用户输入的不同长度向用户显示一些不同的消息。但是我的代码不起作用。这有什么问题?
EditText boxID = findViewById(R.id.vcInput);
Button button = findViewById(R.id.refresh);
TextView tv = findViewById(R.id.message)
final int inputLength = boxID.getText().toString().length();
if(inputLength >= 10 && inputLength <= 11)
{tv.setText("Refresh request sent");}
else if(inputLength<1)
{tv.setText("VC number field looks empty");} //Only this one works at any input length
else if(inputLength>1 && inputLength<10)
{tv.setText("VC number must be at least 10 digit");}
else {tv.setText("Wrong input");}
【问题讨论】:
-
它怎么不特别工作?
-
你需要一个
addTextChangedListener -
只有第一个 Else if 消息(VC 编号字段看起来为空)显示在任何输入长度..
-
删除 toString(); final int inputLength = boxID.getText().length();
-
是的,我放了 OnClickListener,这里只是将代码缩短为我所面临的事实。
标签: java android conditional