2012-11-23 18 views
5

我已经在两个编辑文本上实现了设置错误的代码。错误信号不会消失,搜索正在工作

  • 首先得到没有空值或空值的任何字符串。
  • 其次获取任何没有null或空值的字符串,并且必须有一些Double值。

代码是实现所有的工作会好,但当我不进入后点击搜索按钮的错误后,在第一个编辑框弹出后,如下

import java.util.HashMap; 
import java.util.Map; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class SearchPlaces extends Activity { 
    EditText place; 
    EditText distance; 
    Button search; 
    static String _place; 
    static String _distance; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     place = (EditText) findViewById(R.id.place); 
     distance = (EditText) findViewById(R.id.distance); 
     search = (Button) findViewById(R.id.search); 
     search.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Map<String, String> validate = new HashMap<String, String>(); 
       _place = place.getText().toString(); 
       _distance = distance.getText().toString(); 
       validate.put("placename", _place); 
       validate.put("distancenumeric", _distance); 
       if (!validateInputInformation(validate)) { 
        return; 
       } 
       if ((_place == "" && _distance == "")) { 
        Toast.makeText(getApplicationContext(), 
          "Please fill all the information", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        _place = place.getText().toString(); 
        _distance = distance.getText().toString(); 
        Intent placedist = new Intent(getApplicationContext(), 
          MainActivity.class); 
        Bundle bundle = new Bundle(); 

        bundle.putString("place", _place); 
        bundle.putString("distance", _distance); 

        placedist.putExtras(bundle); 
        startActivity(placedist); 
       } 
      } 

      private boolean validateInputInformation(
        Map<String, String> validate) { 

       String l_place = validate.get("placename"); 
       if (l_place == null || l_place.equals("")) { 
        place.setError("Please enter the Place First"); 
        return false; 
       } 

       String l_distance = validate.get("distancenumeric"); 
       if (l_distance == null || l_distance.equals("")) { 
        distance.setError("Please enter the Distance First"); 
        return false; 
       } 
       if (!isDoubleNumber(l_distance)) { 
        distance.setError("Please enter Numeric Value"); 
        return false; 
       } 

       return true; 

      } 
      private boolean isDoubleNumber (String num) { 
       try { 
        Double.parseDouble(num); 
       } catch (NumberFormatException nfe) { 
        return false; 
       } 
       return true; 
      } 
     }); 
    } 
} 

当我重新输入正确的值作为需要的输入,然后错误通知不会消失,但搜索按钮工作良好。

请帮助我,以便可以从第一个编辑框中删除设置错误通知。

回答

5

写下面的代码行而不是if ((_place == "" && _distance == "")),它会解决你的问题。

if (_place.equals("") && _distance.equals("")) 
+0

不,这解决了我的另一个问题,但仍然有待处理 –

+0

@VarunVishnoi你的第二个问题是什么? –

+0

在第1次编辑文本中给出字符串后,错误红色符号不会消失,但所有工作都很顺利。但那个红色的错误不会消失 –

4

我对你的信息绊倒,因为我面临同样的问题:“当我离开的空场,我SETERROR显示出来,但是当我开始键入,图标和文字不会被删除”

这种行为很奇怪,我敢肯定,这是Android上的一个错误,因为如果我将字段的inputType设置为“textAddressEmail”或“textPassword”,但它不起作用,当我的输入是“text”,“ textCapWords“,”textPersonName“...

解决的办法是以编程方式设置inputType

EditText registrationName = (EditText)findViewById(R.id.registrationName); 

registrationName.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 
+0

这是一个这样一个奇怪的bug大声笑感谢您的帮助,这为我工作。 –

0

我也一直在面对这个问题。要摆脱这个错误,只需致电: myEditText.Error = null;

希望这有助于!

快乐编码!