【问题标题】:Enable submit button after filled all fields in android?填写android中的所有字段后启用提交按钮?
【发布时间】:2015-05-30 03:16:25
【问题描述】:

这是我的代码:这里有一些编辑文本。

public class MainActivity extends ActionBarActivity {

@InjectView(R.id.edt_fname) protected EditText account_fname;
@InjectView(R.id.edt_lnames) protected  EditText account_lname;
@InjectView(R.id.edt_userid) protected EditText account_userid;
@InjectView(R.id.edt_pwd) protected EditText account_password;
@InjectView(R.id.edt_reenter) protected EditText account_reenter_pswd;
@InjectView(R.id.nxt_btn1) protected Button next_acct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);        
    next_acct.setEnabled(false);
    if(( !account_fname.getText().toString().equals("")) &&
            ( !account_lname.getText().toString().equals("")) &&
            ( !account_userid.getText().toString().equals("")) &&
            ( !account_password.getText().toString().equals("")) &&
            ( !account_reenter_pswd.getText().toString().equals("")) )
    {
        next_acct.setEnabled(true);
        next_acct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), PersonalInfo.class));
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }   }

我的目标是当字段为空时,提交按钮被禁用。
如果所有字段都填充了一些文本,则启用提交按钮。

我该怎么做?

【问题讨论】:

    标签: android validation button android-edittext


    【解决方案1】:

    试试这个

    if((account_fname.getText() != null ) &&
            ( account_lname.getText() != null) &&
            ( account_userid.getText() != null) &&
            ( account_password.getText() != null) &&
            ( account_reenter_pswd.getText() != null) )
    {
        next_acct.setEnabled(true);
    }
    

    【讨论】:

    • 我试过了,但是按钮没有被禁用,你有任何示例
    【解决方案2】:

    您在 onCreate 方法中执行此操作。您需要在 EditText 上添加一个 textChangeListener,然后将 if 语句放入 textChangeListener 或创建一个方法,然后将 if 语句放入方法中,然后在 TextChangeListener 中调用该方法。

    TextChangeListener 参考:Counting Chars in EditText Changed Listener

    【讨论】:

    • 我有五个编辑文本字段,所以我可以为所有编辑文本编写文本观察器
    • 文本更改监听器对我非常有用。
    【解决方案3】:

    这很简单……

    if(edttext.getText().toString().equals(edttext2.getText().toString()))
        {
            submit_button.setEnabled(true);
    
        }
        else
        {
            submit_button.setEnabled(true);
        }
    

    【讨论】:

    • 首先检查您的条件,然后为按钮活动或非活动模式编写代码,如上。
    【解决方案4】:

    这里的挑战实际上是收集 EditText 字段的内容。也就是说,一旦你有一个简单的调用来检索所有有趣的 TextView 或 EditText 字段,那么问题就变得微不足道了。

    我对此进行了测试,似乎可行。我相信布局的 EditText 字段的结构或嵌套变化不那么脆弱。

    要回答主题中的问题,如果数组不包含“gatherEditTextContent(View root)”方法的结果中的“null”条目,则可以启用提交按钮。

    public static String[] gatherEditTextContent(View root) {
        final Vector<String> v = new Vector();
        Boolean stop = false;
        scanEditText(root, new ETCallback() {
            @Override
            public boolean onEditText(EditText editText) {
                v.add((editText.getText() == null ? "null" : editText.getText().toString()));
                return false;
            }
        });
        return v.toArray(new String[v.size()]);
    }
    
    /** initiate by passing v= root, callback non-null */
    public static boolean scanEditText(View v, ETCallback callback) {
    
        boolean stop = false;
        if (v instanceof EditText) {
            if (callback != null) stop = callback.onEditText((EditText)v);
        }
        else if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i=0; (!stop && i < vg.getChildCount()); i++) {
                View child = vg.getChildAt(i);
                stop = scanEditText(child, callback);
            }
        }
        return stop;
    }
    
    private interface ETCallback {
        public boolean onEditText(EditText editText);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多