您的位置:首页 > 移动开发 > Android开发

Android朋友圈评论功能知识点记录

2016-01-09 13:16 453 查看
1、输入框获取焦点并弹出输入法

edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
edittext.findFocus();
InputMethodManager inputManager = (InputMethodManager)edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edittext, 0);
2、点击指定布局以外的位置隐藏输入框并收回输入法
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//	        View v = getCurrentFocus();  //点击输入框以外的位置隐藏
View v = linlay_reply;		 //点击指定布局以外的位置隐藏(linlay_reply位一个布局)
if (isShouldHideInput(v, ev)) {

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}

public  boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof LinearLayout)) {
int[] leftTop = { 0, 0 };
//获取输入框当前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
linlay_reply.setVisibility(View.GONE); //隐藏布局
return true;
}
}
return false;
}

其他参考:
http://blog.sina.com.cn/s/blog_ab9fb58a0101kwhc.html http://blog.csdn.net/bear_huangzhen/article/details/45896333
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: