首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当单击PopupWindow的边界外时无法取消

当单击PopupWindow的边界外时无法取消
EN

Stack Overflow用户
提问于 2012-12-07 09:25:43
回答 1查看 808关注 0票数 3

我有这样的代码:

代码语言:javascript
复制
PopupWindow  myMenu = new PopupWindow(menuView, LayoutParams.FILL_PARENT,            LayoutParams.FILL_PARENT,true);  
myMenu.setOutsideTouchable(true);  
myMenu.setBackgroundDrawable(new BitmapDrawable());
myMenu.setTouchable(true);   
myMenu.setFocusable(true); 
myMenu.update();  

如何定义PopupWindow?当PopupWindow弹出时,当我单击后退按钮时,它可以被取消。当我单击PopupWindow的边界外时,它不能被取消。

EN

回答 1

Stack Overflow用户

发布于 2015-01-06 12:47:04

弹出窗口是control.It在android.widget.popupWindow中的一个窗口。我们可以这样用

代码语言:javascript
复制
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener(){



@Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.popup, null);  
             final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT

);



         Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
         btnDismiss.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  popupWindow.dismiss();
 }});

         popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
          alertDialog.setCanceledOnTouchOutside(false);


 }});
    }

下面是popupWindow在popup.xml中的布局

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@android:color/background_light">
     <LinearLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:layout_margin="20dp">
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="It's a PopupWindow" />
      <Button
          android:id="@+id/dismiss"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>

如果有任何疑问问我的意见。普鲁德:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13760213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档