[英]How to check to empty entry in EditText Android
我对此有疑问,我尝试了在我程序的其他部分工作的代码,但这无法正常工作。 我正在编码以确保用户不会提交空白表格。
代号
<EditText
android:id="@+id/msage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext"
android:hint="Write Something...."
android:maxLines="4"
android:elevation="8dp"
android:textSize="14dp"
android:text=""
android:gravity="top"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent"
android:layout_margin="40dp"/>
然后在我的活动:
EditText msg;
msg = (EditText)findViewById(R.id.msage);
String msgContent = msg.getText().toString().trim();
if (msgContent.equals("") || msgContent.length() <= 15) {
msg.setError("Enter valid Message");
return;
}
//---below: code to submit form to server
我什至尝试了TextUtils.isEmpty
编辑
因此,经过12个多小时的拔发,我终于发现逻辑错误。
我的代码包装在
public void btnSendFeedBack(View view) {
// Code
}
使用android:clickable="true" and android:onClick="btnSendFeedBack"
,但这是我发现的错误。
在被@Charuz追赶之后,我变成了
findViewById(R.id.sendFeedback).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
msgContent = msg.getText().toString().trim();
//email
if (msgContent.equals("") || msgContent == null || msgContent.length() < 15) {
msg.setError("Enter a valid Feedback");
return;
}
SendFeedback();
}
});
而且有效。
谢谢您的时间...干杯!!!
您尚未为代码添加其他条件,它会检查空字符串并将其发送到服务器。
if (msgContent.equals("") || msgContent.length() <= 15) {
msg.setError("Enter valid Message");
}
else
{
//submit form to server
}
仅在将表单发送到服务器之前,请验证EditText值。
String val=EditText.getText().toString();
然后在您的按钮内单击使用逻辑,
if(val.length()>15){
// Your logic for submit form to server
}else{
// Error message as per your requirement
}
这是检查Edittext
是否为空的简单逻辑
private boolean isEmpty(EditText etText) {
return etText.getText().toString().trim().length() == 0;
}
在您的代码中尝试以下操作:
String msgContent = msg.getText().toString().trim();
if ((msgContent.length() == 0) || (msgContent.length() <= 15)) {
msg.setError("Enter valid Message");
return;
}
删除EditText中的行:
android:text=""
并尝试:
if(TextUtils.isEmpty(msg.getText().toString()))
首先在if
块上尝试使条件true
,然后将数据发送到服务器:
EditText msg;
msg = (EditText)findViewById(R.id.msage);
String msgContent = msg.getText().toString().trim();
if (!TextUtils.isEmpty(msgContent) || msgContent.length() >= 15) {
//Send data to server
}else{
msg.setError("Enter valid Message");
//return;
}
尝试在代码中使用addTextChangedListener 。
msg.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//here you will get the state of edittext before a change took place
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// here is the best place to check the length of the valu form edit text on the go.
// check whether the length is greater than 15
// and if yes send to server.
//[ MAY BE A BOOLEAN FLAG CAN MAKE YOUR JOB MUCH EASIER]
// Else you can show the error message
if(msg.getText().length()>15){
VALID_MESSAGE = true;
}
else{
msg.setError("Enter valid Message");
VALID_MESSAGE = false;
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
在这里,我使用了布尔值VALID_MESSAGE。 当用户输入长度大于15的文本时,它变为true。当擦除输入的文本时,它也为false。 因此,在发送到服务器时,您只需要检查该标志是否为true即可 。
在afterTextChanged内部也可以这样做。 没有太大的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.