[置顶] 关于软键盘弹出的问题

时间:2021-08-31 13:47:50

功能:当EditText获取焦点时先执行动画,执行完后,弹出软键盘,保证执行动画的流畅度

如果软键盘和动画同时执行,动画有卡的现象,用户体验不好


代码如下:


package com.example.chrombrower.view;

import com.example.chrombrower.MainActivity;
import com.example.chrombrower.R;

import android.content.Context;
import android.os.Handler;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TopEditTextView extends LinearLayout {

private LinearLayout lay;

private Scroller scroller,scroller_lay;

private EditText editText;

private int len;

private int topLen;

private Context c;

public TopEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public TopEditTextView(Context context) {
super(context);
init(context);
}

private void init(Context context){
c=context;
scroller=new Scroller(context);
scroller_lay=new Scroller(context);
View view=LayoutInflater.from(context).inflate(R.layout.top_view,null);
lay=(LinearLayout) view.findViewById(R.id.tv_rl_lay);
editText=(EditText) view.findViewById(R.id.tv_et_content);
editText.setOnTouchListener(listener);
this.addView(view,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
}

private OnTouchListener listener=new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setInputType(InputType.TYPE_NULL);
scroller.startScroll(len, 0, len, 0,500);
scroller_lay.startScroll(0,0, topLen=-lay.getChildAt(2).getWidth()-40,0,1000);
return false;
}
};
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
lay.scrollTo(len=lay.getChildAt(0).getWidth()+5,0);
}

@Override
public void computeScroll() {
if(scroller_lay.computeScrollOffset()){
scrollTo(scroller_lay.getCurrX(),scroller.getCurrY());
//lay.scrollTo(scroller.getCurrX(), scroller.getCurrY());
postInvalidate();
if(scroller_lay.getCurrX()==topLen){
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
super.computeScroll();
}

}