2017-02-25 33 views
1

嗨我在Android的新手,我有3編輯框,我已經設置3基於編輯框明智的setOnFocusChangeListener,當我集中手機沒有編輯框,我得到SIM沒有ID,而不是移動沒有ID。 onFocus函數被錯誤的id調用。如果我有任何的錯誤,請讓我們知道或解決方案。onFocus功能在Android中無法正常工作?

的源代碼:

@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)祛瘀!然後檢查它返回當前焦點textview編號 –

+0

謝謝,它的工作正常..你救了我的一天:)) - – Kalanidhi

回答

1

對於全部EditText,您正在設置相同OnFocusChangeListener。所以當你關注一個新的EditText時,目前的失去了焦點。所以對於這兩個事件onFocusChange()將被調用。但是,您的if條件只會過濾失去焦點的視圖,而不是獲得焦點的視圖。

所以更換

if (!hasFocus) { 

} 

本,使其按預期工作

if (hasFocus) { 

} 
+0

很好的解釋.... – Kalanidhi