EditText doubling out on rotate

梦想的初衷 提交于 2019-11-29 23:01:14
isc.vhs

I had a similar problem but I only see it when the AutoComplete is turned on for the EditText.

My work around was to disable autocomplete. <EditText . . . android:inputType="textMultiLine|textNoSuggestions" />

I came up with a work-around you could try. It works by subclassing EditText, catching a couple of events and then only accepting text changes that occur when the keyboard is shown, which should filter out any changes not made by the user typing something. I still have no idea what could be causing this though.

static class CustomEditText extends EditText{
    boolean keyboardHidden = true;
    String mText = null;
    public CustomEditText(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomEditText(Context context, AttributeSet attr) {
        super(context, attr);
        // TODO Auto-generated constructor stub
    }

    //This gets called for any text field change, regardless of where the change comes from
    //When the phone flips and tries to double the text we can catch it.
    //If the keyboard is hidden (meaning the user isn't typing anything) the strings should match
    protected void onTextChanged(CharSequence text, int start, int before, int after){

        if(keyboardHidden && mText!=null && !text.toString().equals(mText)){
            setText(mText);
            setSelection(mText.length());
        }
        else
            mText = text.toString();
    }

    //There's no way right now to directly check if the soft keyboard is displayed
    //On touch, the soft keyboard is displayed by default for EditText, so assume the keyboard is displayed from this point on
    public boolean onTouchEvent(MotionEvent event){
        keyboardHidden = false;
        super.onTouchEvent(event);
        return true;
    }

    //On a back press we're removing the soft keyboard so set the flag back to true
    public boolean dispatchKeyEventPreIme(KeyEvent event){
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            keyboardHidden = true;
        }
        return super.dispatchKeyEvent(event);
    }   

}
dds

To handle rotation changes yourself add your manifest android:configChanges :

        <activity

        android:name="yourActivity"
        android:configChanges="orientation"></activity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!