Domanda

Despite the question name this really relates more to basic OOP.

Using TextWatcher for form input validation seems (after some research on the matter) to be the most efficient means of validation available on Android for my purposes. I've run into a fairly elementary problem however.

public class MatchConfig extends Activity implements TextWatcher {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_config);
        // Show the Up button in the action bar.
        setupActionBar();

        final EditText teamA = (EditText) findViewById(R.id.teamA_editText); //Team A input
        teamA.addTextChangedListener(this);   //Team A validation
        final EditText teamB = (EditText) findViewById(R.id.teamB_editText);  //Team B input
        teamB.addTextChangedListener(this);   //Team B validation
        final EditText halves = (EditText) findViewById(R.id.halves_editText);  //halves input
        halves.addTextChangedListener(this);   //halves validation
            Button start = (Button) findViewById(R.id.start_button);

start.setOnClickListener(new OnClickListener() {

    // SEND OFF TO DATABASE HANDLING
)}

//Other Stuff
@Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
         //Guard against SQL injection, etc.
        Toast.makeText(this, "after text test", Toast.LENGTH_SHORT).show();

    }


    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "before text test", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "on text test", Toast.LENGTH_SHORT).show();

    }

While this code works, I can't see how to implement different afterTextChanged methods for the separate form elements. The validation will naturally be different for various types of input. While I can overload (as opposed to override) a method like afterTextChanged, I have no means to directly call it through these means (and therefore cannot specify the arguments in order to specifically use the overloaded method).

One small aside: is there any way to cut down on the processing that this implementation will likely demand on the android device? I am worried that such calls for each character of user input will eat up CPU.

È stato utile?

Soluzione

You should create one TextWatcher per EditText, rather than having your Activity implement TextWatcher, in the same way you handled the OnClickListener.

As an aside, you should generally set your TextWatchers in onResume() (or at least, after onRestoreInstanceState()). Otherwise your TextWatcher might fire when the EditText restores the text that was previously entered (in the case that the user changes the device configuration, such as rotating the phone).

Altri suggerimenti

You can have a switch case inside afterTextChanged for ex:

@Override
    public void afterTextChanged(Editable arg0) {
       switch(arg0.getId()){
           case R.id.teamA_editText:

           break;
           case R.id.teamB_editText:

           break;
           case R.id.teamC_editText:

           break;

       }
        Toast.makeText(this, "after text test", Toast.LENGTH_SHORT).show();

    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top