onFocus 功能在 Android 中无法正常工作?

onFocus function not working properly in Android ?

你好,我是 android 的新手,我有 3 个编辑框,我已经根据编辑框明智地设置了 3 个 setOnFocusChangeListener,当我关注移动没有编辑框时,我得到的是 sim no id 而不是 mobile no ID。使用错误的 ID 调用 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>

提前致谢,
卡兰尼迪。

您正在为所有 EditText 设置相同的 OnFocusChangeListener。所以当你聚焦一个新的 EditText 时,当前的就会失去焦点。因此对于这两个事件 onFocusChange() 都将被调用。但是您的 if 条件仅过滤失去焦点的视图,而不过滤获得焦点的视图。

所以替换

if (!hasFocus) {

}

用这个让它按预期工作

if (hasFocus) {

}