允许导航的多个EditText没有用户触摸

时间:2016-03-28 17:42:36

标签: android android-layout android-edittext textwatcher

我有三个EditText Box,它有以下要求:      -

  • 不应允许用户触摸,只能使用“输入键”和“退格键”按钮进行导航。
  • 每个编辑文本可以有2个字符,一旦填满,它应该自动聚焦到右边的下一个EditText框(View.FOCUS_RIGHT)(目前我正在使用TextWatcher来检测它)。

  • 如果用户按下退格键并且当前的EditText框为空,则焦点应移至上一个EditText框(View.FOCUS_LEFT)。

  • 如果当前EditText具有最大字符数(2),并且用户键入新字符,则焦点应转移到下一个(右)EditText,并且新输入的字符应插入到新聚焦的EditText中。 / LI>

以下是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.multitiext.mutlitextviewapp.MainActivity">

<TextView
    android:id="@+id/tv_text_display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tv_text_display"
    android:layout_marginTop="40dp"
    android:orientation="horizontal">

    <com.multitiext.mutlitextviewapp.custom.MyEditText
        android:id="@+id/et_input01"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:inputType="textCapWords"
        android:imeOptions="actionNext"
        android:maxLength="@integer/max_length_per_edit_text"
        android:selectAllOnFocus="true"/>

    <com.multitiext.mutlitextviewapp.custom.MyEditText
        android:id="@+id/et_input02"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:inputType="textCapWords"
        android:imeOptions="actionNext"
        android:maxLength="@integer/max_length_per_edit_text"
        android:selectAllOnFocus="true" />

    <com.multitiext.mutlitextviewapp.custom.MyEditText
        android:id="@+id/et_input03"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:inputType="textCapWords"
        android:imeOptions="actionDone"
        android:maxLength="@integer/max_length_per_edit_text"
        android:selectAllOnFocus="true" />
</LinearLayout>


</RelativeLayout>`

以下是我的三个EditText字段的活动:

`public class MainActivity extends AppCompatActivity {

private MyEditText et_input01;
private MyEditText et_input02;
private MyEditText et_input03;
private TextView tv_display01;
private String fullText;

private View nextET = null;
private EditText currentET = null;
private Integer maxLengthPerEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initUI();
    setInitialFocus();
    setListeners();
    showSoftKeyboard();
}

private void initUI() {
    et_input01 = (MyEditText) findViewById(R.id.et_input01);
    et_input02 = (MyEditText) findViewById(R.id.et_input02);
    et_input03 = (MyEditText) findViewById(R.id.et_input03);
    et_input01.setCurrentET(et_input01);
    et_input02.setCurrentET(et_input02);
    et_input03.setCurrentET(et_input03);
    tv_display01 = (TextView) findViewById(R.id.tv_text_display);
    maxLengthPerEditText = getResources().getInteger(R.integer.max_length_per_edit_text);
}

private void setListeners() {
    InputFilter[] inputFilters = new InputFilter[] {new InputFilter.AllCaps()
    };
    et_input01.setFilters(inputFilters);
    et_input02.setFilters(inputFilters);
    et_input03.setFilters(inputFilters);

    Log.i(MainActivity.class.getName(), "Adding listeners");
    et_input03.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                fullText = (new StringBuilder(et_input01.getText().toString()))
                        .append(et_input02.getText().toString())
                        .append(et_input03.getText().toString())
                        .toString();
                tv_display01.setText(fullText);
            }
            if (event.getKeyCode()==KeyEvent.ACTION_UP && event.getAction() == KeyEvent.KEYCODE_DEL){
                et_input02.requestFocus();
            }
            return false;
        }
    });

    et_input02.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event.getKeyCode()==KeyEvent.ACTION_UP && event.getAction() == KeyEvent.KEYCODE_DEL) {
                et_input01.requestFocus();
            }
            return false;
        }
    });


    et_input01.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            nextET = null;
            currentET = et_input01;
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        @Override
        public void afterTextChanged(Editable s) {
            Log.i("MainActivity", "et01: " + s.toString());
            String currentText = s.toString();
            int currentTextLength = currentText.length();
            String newText = "";
            String moveToNextString = "";

            if (currentTextLength == maxLengthPerEditText) {
                nextET = currentET.focusSearch(View.FOCUS_RIGHT);
            }
            if (currentTextLength == 0) {
            }
            if (currentTextLength > maxLengthPerEditText) {
                newText = (currentText).substring(0, maxLengthPerEditText);
                moveToNextString = (currentText).substring(maxLengthPerEditText);
                currentET.setText(newText);
                nextET = currentET.focusSearch(View.FOCUS_RIGHT);
                if (nextET != null && nextET instanceof EditText) {
                    ((EditText) nextET).setText(moveToNextString);
                }
            }
            if (nextET != null && nextET instanceof EditText) {
                nextET.requestFocus();
            }
        }
    });

    et_input02.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            nextET = null;
            currentET = et_input02;
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        @Override
        public void afterTextChanged(Editable s) {
            Log.i("MainActivity", "et02: " + s.toString());
            String currentText = s.toString();
            int currentTextLength = currentText.length();
            String newText = "";
            String moveToNextString = "";

            if (currentTextLength == maxLengthPerEditText) {
                nextET = currentET.focusSearch(View.FOCUS_RIGHT);
            }
            if (currentTextLength == 0) {
                nextET = currentET.focusSearch(View.FOCUS_LEFT);
            }
            if (currentTextLength > maxLengthPerEditText) {
                newText = (currentText).substring(0, maxLengthPerEditText);
                moveToNextString = (currentText).substring(maxLengthPerEditText);
                currentET.setText(newText);
                nextET = currentET.focusSearch(View.FOCUS_RIGHT);
                if (nextET != null && nextET instanceof EditText) {
                    ((EditText) nextET).setText(moveToNextString);
                }
            }
            if (nextET != null && nextET instanceof EditText) {
                nextET.requestFocus();
            }
        }
    });

    et_input03.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            nextET = null;
            currentET = et_input03;
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        @Override
        public void afterTextChanged(Editable s) {
            Log.i("MainActivity", "et03: " + s.toString());
            String currentText = s.toString();
            int currentTextLength = currentText.length();
            String newText = "";

            if (currentTextLength == maxLengthPerEditText) {
            }
            if (currentTextLength == 0) {
                nextET = currentET.focusSearch(View.FOCUS_LEFT);
            }
            if (currentTextLength > maxLengthPerEditText) {
                newText = (currentText).substring(0, maxLengthPerEditText);
                currentET.setText(newText);
            }
            if (nextET != null && nextET instanceof EditText) {
                nextET.requestFocus();
            }
        }
    });
}

private void setInitialFocus() {
    et_input01.requestFocus();
}

private void showSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

}`

我有一个自定义的EditText来检测退格,但如果当前字段为空,它当前不能用于移动到上一个EditText字段(来自答案:Android EditText delete(backspace) key event)。

public class MyEditText extends EditText {

private MyEditText currentET;

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public MyEditText(Context context) {
    super(context);

}

public MyEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

public void setCurrentET(MyEditText currentET) {
    this.currentET = currentET;
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new MyInputConnection(super.onCreateInputConnection(outAttrs),
            true);
}

private class MyInputConnection extends InputConnectionWrapper {

    public MyInputConnection(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    @Override
    public boolean sendKeyEvent(KeyEvent event) {

        return super.sendKeyEvent(event);
    }

    @Override
    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
        if (beforeLength == 1 && afterLength == 0) {
            return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
        }
        return super.deleteSurroundingText(beforeLength, afterLength);
    }
}

}`

我目前看到的问题是: - 如果EditText为空并且按下退格键,则不会选择先前的EditText。 - 这里有更简单的方法来实现这些要求吗?

0 个答案:

没有答案