因为公司项目的需要,我们要做一个仿照UC的新闻模块中评论功能的实现,也就是在一个Popwindow弹出后,在该弹出框内的EditText自动打开输入框,这边主要的问题是焦点的不同同时获取,后面通过资料查找,EditText通过异步Handle的方法进行处理。
下面就直接上代码,不解释,老司机上车。。
public void onClick(View view) {switch (view.getId()) {case R.id.commentaries_tv://评论 showPopup(view);//显示 popupInputMethodWindow();//异步操作打开软键盘 break;先显示
/** * * @param parent */ private void showPopup(View parent) {View contentViewt = LayoutInflater.from(InformationDetailedActivity.this).inflate(R.layout.dialog_info, null);final PopupWindow popWindow = new PopupWindow(contentViewt,RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,true);editText = (EditText) contentViewt.findViewById(R.id.content_edt);
// popWindow.setAnimationStyle(R.style.popupWindowAnimation);//设置动画
popWindow.setFocusable(true);// 设置允许在外点击消失 popWindow.setOutsideTouchable(false);// 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 popWindow.setBackgroundDrawable(new BitmapDrawable());//软键盘不会挡着popupwindow popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//设置SelectPicPopupWindow弹出窗体的背景 WindowManager.LayoutParams lp = getWindow().getAttributes();lp.alpha = 0.7f;getWindow().setAttributes(lp);//设置菜单显示的位置 popWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);//监听菜单的关闭事件 popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Override public void onDismiss() {WindowManager.LayoutParams lp = getWindow().getAttributes();lp.alpha = 1f;getWindow().setAttributes(lp);}});//监听触屏事件 popWindow.setTouchInterceptor(new View.OnTouchListener() {public boolean onTouch(View view, MotionEvent event) {return false;}});}主要是这段代码哟://进行异步操作 private void popupInputMethodWindow() {Handler handle = new Handler();handle.postDelayed(new Runnable() {@Override public void run() {InputMethodManager inputMethodManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);inputMethodManager.showSoftInput(editText, 0);}}, 0); }主要的代码都再这边了,以后就每周都更,多多学习吧,也是从项目中积累的。