簡體   English   中英

!hasFocus 在我關注那個 Edittext (Android) 后被觸發

[英]!hasFocus to be triggered after I focus on that Edittext (Android)

單擊 Edittext 后,我​​需要顯示錯誤標簽。 例如,在活動開始時,不應顯示錯誤標簽。 當然,重點編輯文本是 edtFirstName,如果我單擊 edtLastName 並將 edtFirstName 留空,它應該會顯示錯誤消息。 與 edtLastName 相同。 我的問題是一開始, edtLastName 的錯誤標簽已經顯示其錯誤標簽“不得為空”。 它甚至在活動開始時就已經調用 onFocusChange,因為焦點位於 edtFirstname。 我該如何解決? 我想做 google add account on android 做的事情。

       public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.content_signup, container, false);

        edtFirstName = (EditText) view.findViewById(R.id.edtFirstName);
        edtLastName = (EditText) view.findViewById(R.id.edtLastName);

        tilFName = (TextInputLayout) view.findViewById(R.id.tilFName);
        tilLName = (TextInputLayout) view.findViewById(R.id.tilLName);

        edtFirstName.setOnFocusChangeListener(this);
        edtLastName.setOnFocusChangeListener(this);

然后

 @Override
    public void onFocusChange(View v, boolean hasFocus) {

        boolean flag = false;

        // First Name
        if (!edtFirstName.hasFocus())
        {
            if (edtFirstName.getText().toString().isEmpty())
            {
                tilFName.setError("Must not be empty.");
            }
            else
            {
                tilFName.setError(null);
            }

        }

        if (!edtLastName.hasFocus()) // this is triggered at the start of the activity. 
        {

            if (edtLastName.getText().toString().isEmpty())
            {
                tilLName.setError("Must not be empty.");
            }
            else
            {
                tilLName.setError(null);
            }

        } 

LostFocus 是否有合適的代碼?

編輯::這是啟動應用程序后發生的事情。 我沒有點擊任何東西。 代碼在啟動時已經調用了 onFocusChange

將此添加到Activity的父XML布局容器:

android:focusable="true"
android:focusableInTouchMode="true"

即,如果您使用的是RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.activities.SampleActivity">

嘗試這個

edtFirstname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
          if (!hasFocus) {
          //do validation here
          }
        }
    });

經過數小時的嘗試,我能夠解決自己的問題

 edtFirstName.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
           edtFirstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
               @Override
               public void onFocusChange(View v, boolean hasFocus) {

                   if (!edtFirstName.hasFocus()) {
                       if (edtFirstName.getText().toString().isEmpty()) {
                           tilFName.setError("Must not be empty.");
                       } else {
                           tilFName.setError(null);
                       }
                   }

               }
           });

            return false;
        }
    });

    edtLastName.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edtLastName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {

                    if(!edtLastName.hasFocus()) {
                        if (edtLastName.getText().toString().isEmpty()) {
                            tilLName.setError("Must not be empty.");
                        } else {
                            tilLName.setError(null);
                        }
                    }
                }
            });

            return false;
        }
    });

也許我必須縮短代碼,因為我還有其他編輯文本在等待。 哈哈。 感謝你們的幫助!

請參閱我的解決方案。 您需要延遲設置 requestFocus 。 如果您滾動查看我的個人資料,則代碼示例是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM