0

我需要专注于所需的编辑框吗?当我单击保存按钮时,我设置提示此字段为必填项,但焦点仍保留在先前的编辑框中我需要的是在该编辑框中获得焦点 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;
                }
            }
4

1 回答 1

0

添加

.requestFocus()

在必填字段上

于 2012-08-22T08:39:16.220 回答