PopupWindows自动弹出软键盘

在网上查了很多方法,自动弹出,都没有成功。猜测和Activity刚初始化,不能弹出键盘的问题应该一样,界面未加载完全。给弹出键盘加一个延时就行了。EditText获取焦点,让其可以弹出软键盘,网上方法很多,而且是正确的,这里就不写了。

先是自定义的popupwindows暴露一个对外的方法:

public void setInput() {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
    imm.showSoftInput(text, 0);
}
 
 

然后在需要的地方调用:

final InvitePopupWindows pop = new InvitePopupWindows(getActivity());
View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_head_web, null);
pop.showAtLocation(contentView, Gravity.CENTER, 0, 0);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        pop.setInput();
    }
}, 100);

这样就成功了,希望能帮到大家。



猜你喜欢

转载自blog.csdn.net/qq_27454233/article/details/80423047