关于软键盘弹出的问题

功能:当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();
	}
	
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值