80,359
社区成员
发帖
与我相关
我的任务
分享
public void showTextPopup(float preX,float preY){
int x = (int) preX;
int y = (int) preY;
EditText mEditText = new EditText(getContext());
mEditText.setText("输入文字");
PopupWindow mTextPopup = new PopupWindow(mEditText, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
//让popup显示在软键盘上面
mTextPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mEditText.requestFocus();
mTextPopup.setOutsideTouchable(true);
//自动弹出软键盘,会导致布局变化,重测量、绘制
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Service.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
mTextPopup.showAtLocation(this, Gravity.LEFT,x,y);
}
case MotionEvent.ACTION_DOWN:
//从前一个点绘制到当前点之后,把当前点定义成下次绘制的一个点
if(currentStyle==3){
showTextPopup(x,y);
}
mTextPopup.showAtLocation(this, Gravity.TOP | Gravity.LEFT, x, y);
原因:
第二个参数:PopupWindow默认是在屏幕的中间。Gravity.TOP | Gravity.RIGHT指的就是屏幕的右上角,那么PopupWindow的中心点坐标是(屏幕宽,0),而Gravity.LEFT表示PopupWindow的中心点坐标是(0,1/2屏幕高),你这里是想中心点坐标是(0,0)那么这样设置Gravity.TOP | Gravity.LEFT就行了