gpt4 book ai didi

android - 使用后退按钮关闭 PopupWindow?

转载 作者:行者123 更新时间:2023-11-29 15:57:27 26 4
gpt4 key购买 nike

我有一个用于显示谷歌地图的弹出窗口,但一旦它打开我似乎无法关闭它,我试图设置后退按钮以关闭它但无法调用 onBackButtonClick当我在 fragment 中时覆盖。虽然似乎没有做任何事情

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);
mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);

mRouteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View popupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);

mPopupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

// If the PopupWindow should be focusable
mPopupWindow.setFocusable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode

后退按钮代码部分:

            mPopupWindow.getContentView().setOnKeyListener( new View.OnKeyListener(){
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ){
if( keyCode == KeyEvent.KEYCODE_BACK ){
mPopupWindow.dismiss();
return true;
}
return false;
}
} );
}
});

好的排序,在遵循 Deepak Singh 的 建议后,我让它工作了,尽管当我重新打开它时,由于尝试重新创建仍然存在的 View 而遇到崩溃问题。这是我的工作代码以供将来引用:

固定版本:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);

mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);

mRouteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPopupView != null) {
ViewGroup parent = (ViewGroup) mPopupView.getParent();
if (parent != null)
parent.removeView(mPopupView);
}
try {
mPopupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
mPopupWindow = new PopupWindow(mPopupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode

mPopupView.findViewById(R.id.doneBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
}
});

最佳答案

在弹出窗口布局上添加一个按钮,使用popupView.findviewbyid(id)找到按钮的id,然后在这个按钮上设置clicklisener并调用mPopupWindow.dismiss();.

你也可以试试这个:

 popupView.setOutsideTouchable(true);
popupView.setFocusable(true);

关于android - 使用后退按钮关闭 PopupWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com