2012-11-23 19 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;

希望這有助於!

快樂編碼!