【问题标题】:Android validation using TextWatcher使用 TextWatcher 进行 Android 验证
【发布时间】:2013-12-01 17:37:01
【问题描述】:

尽管问题名称确实与基本 OOP 相关。

使用 TextWatcher 进行表单输入验证(在对此事进行一些研究之后)似乎是 Android 上可用的最有效的验证方式。然而,我遇到了一个相当基本的问题。

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();

    }

虽然此代码有效,但我看不到如何为单独的表单元素实现不同的 afterTextChanged 方法。对于各种类型的输入,验证自然会有所不同。虽然我可以重载(而不是覆盖)像 afterTextChanged 这样的方法,但我无法通过这些方式直接调用它(因此无法指定参数以专门使用重载方法)。

一个小问题:有没有什么方法可以减少这个实现可能需要在 android 设备上的处理?我担心对用户输入的每个字符的这种调用会占用 CPU。

【问题讨论】:

    标签: android validation overloading textwatcher


    【解决方案1】:

    您应该为每个EditText 创建一个TextWatcher,而不是像处理 OnClickListener 一样让您的 Activity 实现 TextWatcher。

    顺便说一句,您通常应该将TextWatchers 设置在onResume() 中(或至少在onRestoreInstanceState() 之后)。否则你的TextWatcher 可能会在EditText 恢复之前输入的文本时触发(在用户更改设备配置的情况下,例如旋转手机)。

    【讨论】:

      【解决方案2】:

      你可以在 afterTextChanged 里面有一个开关盒,例如:

      @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();
      
          }
      

      【讨论】:

        猜你喜欢
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 2020-01-31
        相关资源
        最近更新 更多