EditTextPopupWindow

软键盘输入框PopupWindow形式适配,自己用怕忘记了记录下

-------------------------Java代码-----------------------------

import android.app.Activity;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.WindowManager;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

import android.widget.PopupWindow;

import com.lumous.unicorn.R;

import java.util.Timer;

import java.util.TimerTask;

public class EditTextPopupWindowextends PopupWindow {

private ViewpopView;

    private Timertimer;

    private ContextmContext;

    public EditTextPopupWindow(Context context){

this.mContext=context;

        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        popView = inflater.inflate(R.layout.pop_view, null);

        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);

        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        this.setContentView(popView);

        // 设置SelectPicPopupWindow弹出窗体可点击

        this.setFocusable(true);

        // 设置SelectPicPopupWindow弹出窗体动画效果

        this.setAnimationStyle(R.style.AnimBottom);

        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        backgroundAlpha(0.4f);

        setOnDismissListener(() -> backgroundAlpha(1f));

    }

private void backgroundAlpha(float alpha){

WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();

        lp.alpha = alpha;

        ((Activity)mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        ((Activity)mContext).getWindow().setAttributes(lp);

    }

/** 打开软键盘 */

    public void openKeyboard(EditText view){

view.setText("");

        keyboard(view);

    }

/** 打开软键盘不清空内容 */

    public void showKeyboard(EditText view){

keyboard(view);

    }

private void keyboard(EditText view){

if(view ==null){

return;

        }

backgroundAlpha(0.4f);

        view.setFocusable(true);

        view.setFocusableInTouchMode(true);

        view.requestFocus();

        if(timer ==null){

timer =new Timer();

        }

timer.schedule(new TimerTask() {

@Override

            public void run() {

InputMethodManager inputMsg = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

                if(inputMsg !=null){

inputMsg.showSoftInput(view, 0);

                }

}

}, 200);

    }

}

------------------------------布局文件------------------------------

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

推荐阅读更多精彩内容