日常问题总结

2017-04-07  本文已影响0人  lipy_

代码修改 drawleft 图标大小

Drawable drawable = getResources().getDrawable(int drawableId);
drawable.setBounds(0, 0, width, height);(一定要先设置这个)
radioButton.setCompoundDrawables(null, null, drawable, null);(要使用这个方法设置图片才能生效)

控制EditText不让输入中文(输入内容类型)

自定义EditText重写onCreateInputConnection()方法
在该方法内返回自定义的MyInputConnecttion

 @Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new MyInputConnecttion(super.onCreateInputConnection(outAttrs),
            false);
}

创建MyInputConnecttion继承InputConnectionWrapper实现InputConnection在commitText()方法内部控制不能输入或者可输入的文字类型

class MyInputConnecttion extends InputConnectionWrapper implements InputConnection {
    public MyInputConnecttion(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    /**
     * 对输入的内容进行拦截
     */
    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
        // 不能输入汉字
        if (text.toString().matches("[\u4e00-\u9fa5]+")) {
            return false;
        }
        return super.commitText(text, newCursorPosition);
    }

}

让popuwindow的父类上的控件处理Touch事件,不让自处理

public static void setPopupWindowTouchModal(PopupWindow popupWindow,
                                            boolean touchModal) {
    if (null == popupWindow) {
        return;
    }
    Method method;
    try {
        method = PopupWindow.class.getDeclaredMethod("setTouchModal",
                boolean.class);
        method.setAccessible(true);
        method.invoke(popupWindow, touchModal);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

android7.0 popuwindow显示位置错误(全屏置顶)

public void showPopupWindow(PopupWindow popupWindow, View view) {
    if (Build.VERSION.SDK_INT < 24) {
        popupWindow.showAsDropDown(view);
    } else {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int y = location[1];
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, y + view.getHeight());
    }
}

上一篇下一篇

猜你喜欢

热点阅读