2017-02-25 14 views
1

こんにちは私はアンドロイドで初心者です、私は3つの編集ボックスがあり、私は3つのsetOnFocusChangeListenerを編集ボックスに基づいて設定しました。 onFocus関数が間違ったIDで呼び出されています。何か間違いがあった場合は、解決策を解決してください。AndroidでonFocus関数が正しく機能していませんか?

ソースコード:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_temp); 

    objMobileNo = (EditText) findViewById(R.id.mobile_no); 
    objSimNo = (EditText) findViewById(R.id.sim_no); 
    objImsiNo = (EditText) findViewById(R.id.imsi_no); 

    View.OnFocusChangeListener a = new View.OnFocusChangeListener() { 
     public void onFocusChange(View view, boolean hasFocus) { 
      if (!hasFocus) { 
       //this if condition is true when edittext lost focus... 
       //check here for number is larger than 10 or not 
       // focusText = "MOBILE"; 

       // Log.d("BKS", "onFocusChange: " + focusText); 
       Log.d("BKS", "onFocusChange: " + view.getId()); 
       Log.d("BKS", "onFocusChange: " + R.id.mobile_no); 



      } 

     } 
    }; 

    objMobileNo.setOnFocusChangeListener(a); 
    objSimNo.setOnFocusChangeListener(a); 
    objImsiNo.setOnFocusChangeListener(a); 

XMLコード:事前に

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<EditText 
    android:layout_marginTop="100dp" 
    android:id="@+id/mobile_no" 
    style="@style/Material_Action" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint_mob_no" 
    android:inputType="number" 
    android:maxLength="10" /> 

<EditText 
    android:id="@+id/sim_no" 
    style="@style/Material_Action" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint_sim_no" 
    android:inputType="number" 
    android:maxLength="20" /> 

<EditText 
    android:id="@+id/imsi_no" 
    style="@style/Material_Action" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint_imsi_no" 
    android:inputType="number" 
    android:maxLength="15" /> 

<EditText 
    android:id="@+id/status" 
    style="@style/Material_Action" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:visibility="gone" /> 

<EditText 
    android:id="@+id/scanning_type" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:visibility="gone" /> 


<LinearLayout 
    android:id="@+id/mLlayoutBottomButtons" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/verify" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_green_light" 
     android:text="Verify" 
     android:textColor="@android:color/white"> 

    </Button> 

</LinearLayout> 

おかげで、
Kalanidhi。

+1

チェックした場合(hasFocus)removとして動作させるために、これと

if (!hasFocus) { } 

を交換してください!それから、現在のフォーカスを返すかどうかをチェックしてください。textview id –

+0

ありがとうございました。私の日を救ってくれました。) - – Kalanidhi

答えて

1

すべてEditTextに同じOnFocusChangeListenerを設定しています。新しいEditTextにフォーカスすると、現在のフォーカスが失われます。だから両方のイベントのためにonFocusChange()が呼び出されます。しかし、あなたのif条件は、フォーカスを失ったビューのみをフィルタリングし、フォーカスを取得したビューはフィルタリングしません。

だから、期待

if (hasFocus) { 

} 
+0

良い説明.... – Kalanidhi

関連する問題