2017-02-05 5 views
1

私はEdittextをクリックした後にエラーラベルを表示する必要があります。 例:アクティビティの開始時に、エラー・ラベルは表示されません。 もちろんedittextはedtFirstNameですが、edtLastNameをクリックしてedtFirstNameを空白のままにするとエラーメッセージが表示されます。 edtLastNameと同じです。私の問題は最初です。edtLastNameのエラーラベルにはすでにエラーラベルが表示されています。 "は空であってはいけません"。フォーカスがedtFirstnameにあるため、アクティビティの開始時にもすでにonFocusChangeが呼び出されています。どうすれば修正できますか?私はアンドロイドのGoogleアカウントを追加することをやりたい!私はそのEdittext(Android)に焦点を当てた後にトリガされるhasFocus

 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のための適切なコードはありますか?

EDIT :: This is what happening after starting the app. I'm not tapping anything. The code already called onFocusChange at the startup

答えて

0

私は待っている他のedittextsを持っているので、たぶん私はコードを短くする必要が

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

をしようと数時間後に私自身の問題を解決することができました。笑。あなたの助けを借りてくれてありがとう!

0

Activityの親XMLレイアウトコンテナにこれを追加:あなたはRelativeLayoutを使用している場合

すなわち
android:focusable="true" 
android:focusableInTouchMode="true" 

<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"> 
0

はこの

をお試しください
edtFirstname.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
      //do validation here 
      } 
     } 
    }); 
+0

edittextをクリックした後にonFocusChangeを呼び出す必要があります(例:edtLNameをクリックします。別のedittextをクリックします。つまり、onFocusChangeをトリガし、edtLNameが空であってはならないというエラーメッセージを表示します)。まだ、edtLNameをタップしていませんが、まだエラーメッセージを表示しています。 –

関連する問題