2011-05-18 59 views
0

是否可以将过滤器(我不确定这个名称)附加到EditTexts,以便如果用户以错误的方式键入某些内容,而不是在EditText上弹出某种类型的警告?EditText的任何内置过滤选项?

例如:表单上有一个EditText,它必须填充。因此,直到它为空时,其中会显示一些警告,表明它是表单上的必填字段。

我在EditTexts上看到了类似Android的行为,所以我想这里面有一个内置功能,但是我找不到它。请有人帮助我吗?

目标Android版本是3.0以上。

回答

1

您可能需要添加TextWatcher,并在实施public void afterTextChanged(Editable s)时做出相应的反应。只需检查s是否为空,或者是否正确。

你也可以执行filters,但那些通常是以阻止用户输入错误的数据。

1

在xml中查看EditTextandroid:inputType属性。例如android:inputType="textEmailAddress|number|phone"等东西。
但是为了检查空虚,我不能建议任何东西,但if (editText.getText().length() > 0)

1

您可以使用带有文本观察器事件的代码添加过滤器。当你在编辑文本上键入任何想法时,文本观察器事件就会执行,那么如果条件为假,我们可以在编辑文本中检查类型文本,那么你应该显示弹出窗口。我在列表视图中显示了一个使用编辑文本进行过滤的例子,在文本监视器事件的帮助下,您应该使用edittext进行过滤。

ed.addTextChangedListener(新TextWatcher(){

 public void afterTextChanged(Editable s) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      fillMaps.clear(); 
      textlength = ed.getText().length(); 
      for (int i = 0; i < countryName.length; i++) { 
       if (textlength <= countryName[i].length()) { 
        if (ed.getText() 
          .toString() 
          .equalsIgnoreCase(
            (String) countryName[i].subSequence(0, 
              textlength))) { 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put("flag", "" + imageId[i]); 
         map.put("country", countryName[i].toString()); 
         map.put("capital", capitalName[i].toString()); 
         map.put("countrytime", 
           convertDateTimeToGMT(
             GMTplusMinusInMillisecond[i], 
             plusMinus[i])); 
         map.put("GMT", GMTplusMinus[i].toString()); 

         fillMaps.add(map); 
        } 
       } 
      } 
      SimpleAdapter adapter = new SimpleAdapter(
        WorldClockActivity.this, fillMaps, R.layout.grid_item, 
        from, to); 
      lv1.setAdapter(adapter); 
      // lv1.setAdapter(new 
      // ArrayAdapter<String>(WorldClockActivity.this,android.R.layout.simple_list_item_1 
      // , arr_sort)); 

     } 
    }); 
相关问题