lundi 3 juillet 2017

Compare text in editText with String JAVA ANDROID

In my example I have a Button, an EditText and a String. If I press the Button following should happen.

1.Compare all chars in EditText (that the user has typed in) with the chars from the String. If a char in the EditText is not equal to the char from the String (at the position i) replace this char and all the following chars in editText with the the only the char from the String (at position i).

2.However, if editText is already containing the full string but has a spelling mistake replace the text in editText with the correct String.

I hope that somebody can follow my thoughts and I would highly appreciate any help on this matter!

This is my code so far, but it doesn't really seem to work..

         public class FirstActivity extends AppCompatActivity{

             Button help;
             EditText editText;
             String name = "Bart Simpson";

          @Override
             protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.first_layout);


help.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                 for(int i=0; i<name.length; i++){
if(editText.getText().toString().charAt(i).equals(name.charAt(i)){   
}else{
editText.replace(all letters from editText from the position i with name.charAt(i);
        break;}
                 }
}
}}

Aucun commentaire:

Enregistrer un commentaire