PopupWindow显示时无法单击父活动按钮

时间:2012-12-18 11:39:30

标签: android popup

在我的父活动我有一个按钮,当我点击它时显示PopUpWindow有2个ImageButton ..当这个PopUpWindow存在时'我无法点击我的父活动按钮..这是我的代码,是否有任何问题..

public class PopUpExample extends Activity {

    Button but;
    LinearLayout mainLayout;
    PopupWindow popUp;
    boolean click = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      

        mainLayout = (LinearLayout) findViewById(R.id.main_layout);     
        but = (Button) findViewById(R.id.main_btn);     

        but.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                View popView;

                if(click){
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                    popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100, 50, true);
                    popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
                    popUp.update();
                    click = false;

                    popView = popUp.getContentView();

                    ImageButton call = (ImageButton) popView.findViewById(R.id.call_btn);   

                    call.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {                           
                            Toast.makeText(PopUpExample.this, "Calling...", Toast.LENGTH_SHORT).show();
                        }
                    });

                    ImageButton sms = (ImageButton) popView.findViewById(R.id.sms_btn); 

                    sms.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {                           
                            Toast.makeText(PopUpExample.this, "Sms...", Toast.LENGTH_SHORT).show();
                        }
                    });

                }else{
                    popUp.dismiss();
                    click = true;
                }                       

            }    
        });
    }    
}

5 个答案:

答案 0 :(得分:3)

创建时的popview会从mainView中删除焦点,因此用户无法单击主视图上的元素。
要点击主视图,首先需要解除popview。
关于代码中的上述理论您试图通过单击主要活动中不可能的按钮来解除popview。

下面的代码包含您需要在上面的代码中包含的更改

public class PopUpExample extends Activity {    
    Button but;
    LinearLayout mainLayout;
    PopupWindow popUp;
    //boolean click = true;
    View popView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLayout = (LinearLayout) findViewById(R.id.main_layout);     
        but = (Button) findViewById(R.id.main_btn);     

        but.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // if(click){
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                    popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100, 50, true);
                    popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
                    popUp.update();
                    //click = false;

                    popView = popUp.getContentView();

                    ImageButton call = (ImageButton)popView.findViewById(R.id.call_btn);   

                    call.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {                           
                            Toast.makeText(MainActivity.this, "Calling...", Toast.LENGTH_SHORT).show();
                            popUp.dismiss();
                        }
                    });

                    ImageButton sms = (ImageButton)popView.findViewById(R.id.sms_btn); 

                    sms.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {                           
                            Toast.makeText(MainActivity.this, "Sms...", Toast.LENGTH_SHORT).show();
                            popUp.dismiss();
                        }
                    });

               //}else{
                   // popUp.dismiss();
                 // click = true;
               // }                       

            }    
        });
}
}

答案 1 :(得分:0)

要启用活动按钮,您必须关闭弹出窗口。

答案 2 :(得分:0)

感谢您的回答,很抱歉回答我的问题..在这一行

popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100, 50, true);

意思是......

PopupWindow(View contentView, int width, int height, boolean focusable);

我将focusable设置为true,因此它的阻塞父视图..所以当我将其设置为false时,我可以访问单击父视图按钮.. :)

答案 3 :(得分:0)

另一种最简单的方法是

    public class MainActivity extends Activity {

      Button but;
     LinearLayout mainLayout;
     PopupWindow popUp;

        @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);      
        mainLayout = (LinearLayout) findViewById(R.id.lin);   
        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.activity_main, null);  
             final PopupWindow popupWindow = new PopupWindow (popupView,   
             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
             popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
       ImageButton call = (ImageButton)   
             popupView.findViewById(R.id.imageButton1);   

    call.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {                           
            Toast.makeText(MainActivity.this, "Calling...", Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();
        }
    });

    ImageButton sms = (ImageButton) popupView.findViewById(R.id.imageButton2); 

    sms.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {                           
            Toast.makeText(MainActivity.this, "Sms...", Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();
        }
    });


   }
   });
    }
}

main2.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:id="@+id/lin" >


<Button
    android:id="@+id/openpopup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Open Popup Window" />

activity_main.xml中

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical" 
  android:background="@android:color/background_light">

    <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

   <ImageButton
         android:id="@+id/imageButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/ic_launcher" />


      </LinearLayout>

答案 4 :(得分:0)

这个正在为我工​​作设置popupWindow.setFocusable(false);,后台和popwindow dismiss将同时工作