Android 表单:获取焦点文本字段 ID

Android Forms : Get focused text field id

我有两个文本字段。我专注于第一个文本字段。如果我单击一个按钮,我应该能够找到哪个文本字段具有焦点并想知道它的 ID。

<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/editText2"
  android:layout_below="@+id/editText"
  android:layout_alignParentRight="true"
  android:layout_alignParentEnd="true"
  android:layout_marginTop="33dp"
  android:hint= "@string/rfid_plh"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true" />

<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/editText2"
  android:layout_below="@+id/editText"
  android:layout_alignParentRight="true"
  android:layout_alignParentEnd="true"
  android:layout_marginTop="33dp"
  android:hint= "@string/rfid1_plh"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true" />

如何实现?

这将为您完成:

    final EditText et1,et2;
    Button checkButton;

    //initilize both edit text and button here

    checkButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(et1.hasFocus()){
                //TODO code here for edit text 1
                int id = et1.getId();
            }else if(et2.hasFocus()){
                //TODO code here for edit text 2
                int id = et2.getId();

            }
        }
    });