onFocusChange做点什么,然后将焦点设置回相同的视图

时间:2018-07-22 11:34:06

标签: android android-layout

我正在尝试进行一项可与蓝牙条形码读取器一起使用的活动。

我希望能够选中此复选框,然后在功能键上按“下一步”,将焦点移至etReturnsUniqueNo。然后,我将扫描条形码,该条形码将输入数字并将焦点移至下一个输入。我希望输入的详细信息然后显示在etReturnsLast中,并将焦点设置回etReturnsUniqueNo。

问题是我正在执行双光标操作,etReturnsUniqueNo和etReturnsLast都显示了光标,而etReturnsLast是输入下一个文本的光标,不确定为什么吗?谁能解释发生了什么,也许告诉我应该怎么办?

MainActivity.Java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity {

    private CheckBox chkReturnsInstore;
    private EditText etReturnsUniqueNo;
    private EditText etReturnsLast;

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

        initViews();
        initListeners();
        initObjects();

        Returns();
    }

    private void initViews() {

        chkReturnsInstore = (CheckBox) findViewById(R.id.chkReturnsInstore);
        etReturnsUniqueNo = (EditText) findViewById(R.id.etReturnsUniqueNo);
        etReturnsLast = (EditText) findViewById(R.id.etReturnsLast);

    }

    /**
     * This method is to initialize listeners
     */
    private void initListeners() {    }

    private void initObjects(){    }

    public void Returns() {

        //OnUniqueNumber Lost focus
        etReturnsUniqueNo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus){
                    String[] data = new String[2];

                    data[0] = etReturnsUniqueNo.getText().toString().trim();
                    if (chkReturnsInstore.isChecked()){
                        data[1] = "Y";
                    }else {
                        data[1] = "N";
                    }

                    //Show details in etNewLast
                    String line = "";
                    String newline = "";
                    line = etReturnsLast.getText().toString();
                    for (int i=0; i<2;i++) {
                        if(i<1) {
                            newline = newline + data[i] + ",";
                        }else{
                            newline = newline + data[i] + "\n";
                        }
                    }
                    newline = newline + line;
                    etReturnsLast.setText(newline);
                    etReturnsUniqueNo.setText("");
                    etReturnsUniqueNo.requestFocus();
                }
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/clStorageMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.constraint.ConstraintLayout
    android:id="@+id/clStorageReturns"
    android:layout_width="match_parent"
    android:layout_height="462dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="@+id/clStorageMenu"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/clStorageMenu"
    app:layout_constraintVertical_bias="0.0">

    <CheckBox
        android:id="@+id/chkReturnsInstore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/etReturnsUniqueNo"
        android:layout_width="198dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chkReturnsInstore" />

    <EditText
        android:id="@+id/etReturnsLast"
        android:layout_width="396dp"
        android:layout_height="284dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ems="10"
        android:gravity="bottom"
        android:inputType="textMultiLine"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:verticalScrollbarPosition="defaultPosition"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etReturnsUniqueNo"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/tvReturnsInstore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="Instore?"
        app:layout_constraintEnd_toStartOf="@+id/chkReturnsInstore"
        app:layout_constraintHorizontal_bias="0.772"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvReturnsUniqueNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Unique Number"
        app:layout_constraintBottom_toTopOf="@+id/etReturnsLast"
        app:layout_constraintEnd_toStartOf="@+id/etReturnsUniqueNo"
        app:layout_constraintHorizontal_bias="0.636"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvReturnsInstore"
        app:layout_constraintVertical_bias="0.109" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

以下代码将光标发送回etReturnsUniqueNo。放在其他听众下方。

    etReturnsLast.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus){
                etReturnsUniqueNo.requestFocus();
            }
        }
    });