日常问题总结

代码修改 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());
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,569评论 25 708
  • 总结一些遇到的问题。日常工作中用到的一些方法总结,有很简单介绍,可能也有错误,如果您看到了希望可以告诉我,会不间断...
    最后还是个农阅读 1,681评论 4 7
  • 1.正则表达式中\w \w表示的是匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。因此,需要判断...
    星月西阅读 184评论 0 0
  • 1.presentViewController和pushViewController区别:presentViewC...
    木子尚武阅读 283评论 0 1
  • 诗曰:几度梦中与君同,千里江南无君影。可怜不与离人遇,梦醒时分空悲泣。 1、梦中勾画 时间过去那么久了,心里还总是...
    葉糖糖阅读 224评论 0 2