2016-08-11 19 views
1

私は自分のアプリにEdittextを持っていて、最初の文字を空白として入力しないようにしたいと思っていますが、他の文字を入力した後にはどうすればいいですか?>EditTex Validation with android

android:id="@+id/referralCode" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_gravity="center_horizontal" 
 
       android:layout_marginTop="8dp" 
 
       android:background="@drawable/formfield_1" 
 
       android:hint="@string/referralcode_hint" 
 
       android:imeOptions="actionNext"/>

とJavaクラスで私は入力フィルタがあります。

public static InputFilter alphabetsFilter = new InputFilter() { 
 

 
\t @Override 
 
\t public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
 
\t  if (source.equals("")) { // for backspace 
 
\t \t return source; 
 
\t  } 
 
\t  if (source.toString().matches("[a-zA-Z ]+")) { 
 
\t \t return source; 
 
\t  } 
 
\t  return ""; 
 
\t } 
 
    };
0123を入力フィルタで

inal EditText editText = (EditText)findViewById(R.id.editText); 


    InputFilter filter = new InputFilter() { 
     boolean canEnterSpace = false; 

     public CharSequence filter(CharSequence source, int start, int end, 
       Spanned dest, int dstart, int dend) { 

      if(editText.getText().toString().equals("")) 
      { 
       canEnterSpace = false; 
      } 

      StringBuilder builder = new StringBuilder(); 

      for (int i = start; i < end; i++) { 
       char currentChar = source.charAt(i); 

       if (Character.isLetterOrDigit(currentChar) || currentChar == '_') { 
        builder.append(currentChar); 
        canEnterSpace = true; 
       } 

       if(Character.isWhitespace(currentChar) && canEnterSpace) { 
        builder.append(currentChar); 
       } 


      } 
      return builder.toString();   
     } 

    }; 


    editText.setFilters(new InputFilter[]{filter}); 
+0

をString test = "Stack"のような文字列の最初の文字を検証できます。 char first = test.charAt(0); – Singh

答えて

1

を使用します。

yourEditText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if(!(s.toString().charAt(0) == ' ')) { 
      //do whatever you want 
      //you can show an alert dialog saying the first character should not be a whitespace 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
0

あなたはこれを試すことができますを入力できるようにしたい

private static class AlphaNumericInputFilter implements InputFilter { 
    public CharSequence filter(CharSequence source, int start, int end, 
           Spanned dest, int dstart, int dend) { 
     // Only keep characters that are alphanumeric 
     StringBuilder builder = new StringBuilder(); 
     for (int i = start; i < end; i++) { 
      char c = source.charAt(i); 
      if (Character.isLetterOrDigit(c) || !Character.isSpaceChar(c)) { 
       builder.append(c); 
      } 
     } 
     // If all characters are valid, return null, otherwise only return the filtered 
     // characters 
     boolean allCharactersValid = (builder.length() == end - start); 
     return allCharactersValid ? null : builder.toString(); 
    } 
} 

このフィルタの新しいインスタンスを作成し、edittextに追加します。 XMLで

+0

それは私のために動作しません – Katherina

0

ループ、など必要な文字を追加します。最初の空白文字を許可するdoesntのが、私はそれが数字や特殊文字

1

あなたはこのようCustomEditTextクラスを作ることができます...

public class CustomEditText extends EditText { 
    public CustomEditText(Context context) { 
     super(context); 
    } 

    public CustomEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 
     super.onTextChanged(text, start, lengthBefore, lengthAfter); 
     if (text.length() == 1) { 
      if (Character.isSpaceChar(text.charAt(0))) { 
       this.setText(""); 
      } 
     } 
    } 
} 

、代わりのEditTextの何を行うことができますが、このようなaddTextChangedListener設定されている。このCustomEditText

+0

コードを更新しました。 –

関連する問題