如何在EditText中为不同的输入长度显示不同的消息?

我想向用户显示一些不同的消息,用于他们所做的不同长度的输入。但是我的代码不起作用。这是怎么回事?


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

http://img3.mukewang.com/62fcb4020001d84107810635.jpg

慕姐4208626
浏览 134回答 3
3回答

慕姐8265434

你要找的,可能是TextWatcher使用示例:boxID.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count, int after) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void afterTextChanged(Editable s) {&nbsp; &nbsp; &nbsp; &nbsp; int inputLength = s.length();&nbsp; &nbsp; &nbsp; &nbsp; if(inputLength >= 10 && inputLength <= 11)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {tv.setText("Refresh request sent");}&nbsp; &nbsp; &nbsp; &nbsp; else if(inputLength<1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {tv.setText("VC number field looks empty");}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else if(inputLength>1 && inputLength<10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {tv.setText("VC number must be at least 10 digit");}&nbsp; &nbsp; &nbsp; &nbsp; else {tv.setText("Wrong input");}&nbsp; &nbsp; });

GCT1015

好的,你有3个条件(实际上是4个)和inputLength >= 10 && inputLength <= 11inputLength<1inputLength>1 && inputLength<10else你可以让它像这样:if(inputLength >= 10 && inputLength <= 11)&nbsp; {tv.setText("Refresh request sent");}if(inputLength<1)&nbsp; &nbsp; &nbsp; &nbsp;{tv.setText("VC number field looks empty");}if(inputLength>1 && inputLength<10)&nbsp; &nbsp; &nbsp; &nbsp;{tv.setText("VC number must be at least 10 digit");}if(inputLength > 11)&nbsp; &nbsp;{tv.setText("Wrong input");}我在陈述方面也有类似的问题。我不得不重建我的条件树。else if

摇曳的蔷薇

你是.最初,输入长度为空,它被保存到值为 0 的最终字段中。这就是为什么只有第二个条件被触发。inputLengthfinalinputLength该值是在触发之前设置的。inputLengthonClick一个解决方案要么是使用,要么是一个更简单的解决方案,只是在里面移动,这样你就可以为每个触发器获得一个新的输入长度值:TextWatcherinputLengthonClickonClick&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; final int inputLength = boxID.getText().toString().length();&nbsp; &nbsp; &nbsp; &nbsp;new Handler().postDelayed(new Runnable() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(inputLength >= 10 && inputLength <= 11)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setText("Refresh request sent");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(inputLength<1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setText("VC number field looks empty"); //Only this one works at any input length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(inputLength>1 && inputLength<10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setText("VC number must be at least 10 digit");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else tv.setText("Wrong input");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java