如何在验证用户验证时关注编辑框?

时间:2012-08-22 08:25:10

标签: android xml

我需要关注所需的编辑框? 当我点击保存按钮时,我设置提示此字段是必需的,但焦点仍然保留在之前的编辑框中我需要的是在该编辑框中获得焦点本身setfocusable(true)不适用于此 这是代码`

save.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                boolean valid = getvalid();
                if (valid == false) {
                    String field = getField();

                    Toast.makeText(writeRFIDTag.this,
                            "field " + field + " required", Toast.LENGTH_SHORT)
                            .show();
                }`

和getfield方法

private String getField() {
                String empty = null;
                String required = "this field required";
                String mUser = userId.getText().toString();
                if (mUser.isEmpty()) {
                    empty = "User Id";
                    userId.setHint(required);
                } else {

                    String mCompany = company.getText().toString();
                    if (mCompany.isEmpty()) {
                        empty = "Company";
                        company.setHint(required);
                    } else {

                        String mFirstName = firstName.getText().toString();
                        if (mFirstName.isEmpty()) {
                            empty = "First Name";
                            firstName.setHint(required);
                        } else {
                            String mLastName = lastName.getText().toString();
                            if (mLastName.isEmpty()) {
                                empty = "Last Name";
                                lastName.setHint(required);
                            } else {

                                String mEncryptionKey = encryptionKey.getText()
                                        .toString();
                                if (mEncryptionKey.isEmpty()) {
                                    empty = "encryption key";
                                    encryptionKey.setHint(required);
                                }
                            }
                        }
                    }
                }

                return empty;
            }

and getvalid method

private boolean getvalid() {

                String mUser = userId.getText().toString();
                // if (mUser == null)
                // userId.setError("First name is required!");
                String mCompany = company.getText().toString();
                // if (mCompany == null)
                // userId.setError("First name is required!");
                String mFirstName = firstName.getText().toString();
                // if (mFirstName == null)
                // userId.setError("First name is required!");
                String mLastName = lastName.getText().toString();
                // if (mLastName == null)
                // userId.setError("First name is required!");
                String mEncryptionKey = encryptionKey.getText().toString();
                // if (mEncryptionKey == null)
                // userId.setError("First name is required!");

                if (mUser.equalsIgnoreCase("") || mCompany.equalsIgnoreCase("")
                        || mFirstName.equalsIgnoreCase("")
                        || mLastName.equalsIgnoreCase("")
                        || mEncryptionKey.equalsIgnoreCase(""))

                    return false;
                else {
                    return true;
                }
            }

1 个答案:

答案 0 :(得分:0)

添加

.requestFocus()

在必填字段