下面列出了android.widget.PopupWindow#setInputMethodMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("ClickableViewAccessibility")
private void create() {
mPopupWindow = new PopupWindow(mDropPopLayout, LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOnDismissListener(new PopOnDismissListener());
mDropPopLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mPopupWindow.dismiss();
return true;
}
});
}
private void apply(PopupWindow mPopupWindow) {
mPopupWindow.setClippingEnabled(this.mClippEnable);
if(this.mIgnoreCheekPress) {
mPopupWindow.setIgnoreCheekPress();
}
if(this.mInputMode != -1) {
mPopupWindow.setInputMethodMode(this.mInputMode);
}
if(this.mSoftInputMode != -1) {
mPopupWindow.setSoftInputMode(this.mSoftInputMode);
}
if(this.mOnDismissListener != null) {
mPopupWindow.setOnDismissListener(this.mOnDismissListener);
}
if(this.mOnTouchListener != null) {
mPopupWindow.setTouchInterceptor(this.mOnTouchListener);
}
mPopupWindow.setTouchable(this.mTouchable);
}
/**
* 弹出popupWindow
* Show popup.
*
* @param gravity the gravity
* @param width the width
* @param height the height
* @param animationStyle the animation style
*/
private void showPopup(int gravity, int width, int height, int animationStyle) {
mPopupView = LayoutInflater.from(mActivity).inflate(R.layout.popup_demo, null);
mPopupWindow = new PopupWindow(mPopupView, width, height);
//以下属性响应空白处消失和实体按键返回消失popup
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//沉浸式模式下,以下两个属性并不起作用
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//重点,此方法可以让布局延伸到状态栏和导航栏
mPopupWindow.setClippingEnabled(false);
//设置动画
mPopupWindow.setAnimationStyle(animationStyle);
//弹出
mPopupWindow.showAtLocation(getWindow().getDecorView(), gravity, 0, 0);
//弹出后背景alpha值
backgroundAlpha(0.5f);
//消失后恢复背景alpha值
mPopupWindow.setOnDismissListener(() -> backgroundAlpha(1f));
//适配弹出popup后布局被状态栏和导航栏遮挡问题
updatePopupView();
}
public void show(@NonNull final EmojiImageView clickedImage, @NonNull final Emoji emoji) {
dismiss();
rootImageView = clickedImage;
final View content = initView(clickedImage.getContext(), emoji, clickedImage.getWidth());
popupWindow = new PopupWindow(content, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
popupWindow.setBackgroundDrawable(new BitmapDrawable(clickedImage.getContext().getResources(), (Bitmap) null));
content.measure(makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
final Point location = Utils.locationOnScreen(clickedImage);
final Point desiredLocation = new Point(
location.x - content.getMeasuredWidth() / 2 + clickedImage.getWidth() / 2,
location.y - content.getMeasuredHeight()
);
popupWindow.showAtLocation(rootView, Gravity.NO_GRAVITY, desiredLocation.x, desiredLocation.y);
rootImageView.getParent().requestDisallowInterceptTouchEvent(true);
Utils.fixPopupLocation(popupWindow, desiredLocation);
}
EmojiPopup(@NonNull final EmojiPopup.Builder builder, @NonNull final EditText editText) {
this.context = Utils.asActivity(builder.rootView.getContext());
this.rootView = builder.rootView.getRootView();
this.editText = editText;
this.recentEmoji = builder.recentEmoji;
this.variantEmoji = builder.variantEmoji;
popupWindow = new PopupWindow(context);
variantPopup = new EmojiVariantPopup(rootView, internalOnEmojiClickListener);
final EmojiView emojiView = new EmojiView(context,
internalOnEmojiClickListener, internalOnEmojiLongClickListener, builder);
emojiView.setOnEmojiBackspaceClickListener(internalOnEmojiBackspaceClickListener);
popupWindow.setContentView(emojiView);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null)); // To avoid borders and overdraw.
popupWindow.setOnDismissListener(onDismissListener);
if (builder.keyboardAnimationStyle != 0) {
popupWindow.setAnimationStyle(builder.keyboardAnimationStyle);
}
rootView.addOnAttachStateChangeListener(onAttachStateChangeListener);
}
/**
* 添加一些属性设置
* @param popupWindow
*/
private void apply(PopupWindow popupWindow){
popupWindow.setClippingEnabled(mClippEnable);
if(mIgnoreCheekPress){
popupWindow.setIgnoreCheekPress();
}
if(mInputMode!=-1){
popupWindow.setInputMethodMode(mInputMode);
}
if(mSoftInputMode!=-1){
popupWindow.setSoftInputMode(mSoftInputMode);
}
if(mOnDismissListener!=null){
popupWindow.setOnDismissListener(mOnDismissListener);
}
if(mOnTouchListener!=null){
popupWindow.setTouchInterceptor(mOnTouchListener);
}
popupWindow.setTouchable(mTouchable);
}
/**
* Create a new, empty popup window capable of displaying items from a ListAdapter.
* Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}.
*
* @param context Context used for contained views.
*/
AutocompletePopup(@NonNull Context context) {
super();
mContext = context;
mPopup = new PopupWindow(context);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
private void init() {
mCommandAdapter = new CommandListSuggestionsAdapter(getContext());
mCommandAdapter.setClickListener(this);
List<CommandAliasManager.CommandAlias> additionalItems = new ArrayList<>();
additionalItems.add(CommandAliasManager.CommandAlias.raw("wait", "<seconds>", ""));
additionalItems.add(CommandAliasManager.CommandAlias.raw("wait-for", "<channel>", ""));
mCommandAdapter.setAdditionalItems(additionalItems);
mSuggestionsList = new RecyclerView(getContext());
mSuggestionsList.setAdapter(mCommandAdapter);
mSuggestionsList.setLayoutManager(new LinearLayoutManager(getContext()));
mPopupAnchor = new View(getContext());
mPopupWindow = new PopupWindow(getContext(), null, android.R.attr.listPopupWindowStyle);
mPopupWindow.setContentView(mSuggestionsList);
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupItemHeight = StyledAttributesHelper.getDimensionPixelSize(getContext(),
android.R.attr.listPreferredItemHeightSmall, 0);
mMaxPopupHeight = getResources().getDimensionPixelSize(R.dimen.list_popup_max_height);
addTextChangedListener(new SimpleTextWatcher((Editable s) -> {
if (enoughToFilter())
performFiltering(false);
else
dismissDropDown();
}));
}
public SearchResultPop(Context context, final FileListAdapter adapter){
this.adapter = adapter;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.search_result_pop, null);
GridView gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(adapter);
gridView.requestFocus();
// EditText 与 popwindow焦点冲突
// gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// popupWindow.setFocusable(true);
// listener.onClick((FileInfo)adapter.getItem(arg2), arg2);
// }
// });
popupWindow = new PopupWindow(view,
windowManager.getDefaultDisplay().getWidth(),
windowManager.getDefaultDisplay().getHeight()*4/5);
// 使其聚集
//popupWindow.setFocusable(true);
// 解决popupWindow挡住虚拟键盘
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(false);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
}
private void init() {
setupColors();
setupList();
mSearchEditText.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
mStartSearchImageView.setOnClickListener(this);
mDoneSearchImageView.setOnClickListener(this);
mSearchEditText.addTextChangedListener(mTextWatcher);
mPopupWindow = new PopupWindow(mContext);
mPopupWindow.setContentView(mSpinnerListContainer);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
hideEdit();
}
});
mPopupWindow.setFocusable(false);
mPopupWindow.setElevation(DefaultElevation);
mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.spinner_drawable));
mSpinnerListView.setOnItemClickListener(mOnItemSelectedListener);
if (mCurrSelectedView == null) {
if (!TextUtils.isEmpty(mSearchHintText)) {
mSearchEditText.setHint(mSearchHintText);
}
if (!TextUtils.isEmpty(mNoItemsFoundText)) {
mEmptyTextView.setText(mNoItemsFoundText);
}
if (mCurrSelectedView == null && !TextUtils.isEmpty(mRevealEmptyText)) {
TextView textView = new TextView(mContext);
textView.setText(mRevealEmptyText);
mCurrSelectedView = new SelectedView(textView, -1, 0);
mRevealItem.addView(textView);
}
} else {
mSpinnerListView.performItemClick(mCurrSelectedView.getView(), mCurrSelectedView.getPosition(), mCurrSelectedView.getId());
}
clearAnimation();
clearFocus();
}
private void init() {
setupColors();
setupList();
mSearchEditText.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
mStartSearchImageView.setOnClickListener(this);
mDoneSearchImageView.setOnClickListener(this);
mSearchEditText.addTextChangedListener(mTextWatcher);
mPopupWindow = new PopupWindow(mContext);
mPopupWindow.setContentView(mSpinnerListContainer);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
hideEdit();
}
});
mPopupWindow.setFocusable(false);
mPopupWindow.setElevation(DefaultElevation);
mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.spinner_drawable));
mSpinnerListView.setOnItemClickListener(mOnItemSelectedListener);
if (mCurrSelectedView == null) {
if (!TextUtils.isEmpty(mSearchHintText)) {
mSearchEditText.setHint(mSearchHintText);
}
if (!TextUtils.isEmpty(mNoItemsFoundText)) {
mEmptyTextView.setText(mNoItemsFoundText);
}
if (mCurrSelectedView == null && !TextUtils.isEmpty(mRevealEmptyText)) {
TextView textView = new TextView(mContext);
textView.setText(mRevealEmptyText);
mCurrSelectedView = new SelectedView(textView, -1, 0);
mRevealItem.addView(textView);
}
} else {
mSpinnerListView.performItemClick(mCurrSelectedView.getView(), mCurrSelectedView.getPosition(), mCurrSelectedView.getId());
}
clearAnimation();
clearFocus();
}
public IcsListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
mContext = context;
mPopup = new PopupWindow(context, attrs, defStyleAttr);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}