1

在我的父活动中,我有一个按钮,当我单击它时,会显示带有 2 个 ImageButton 的 PopUpWindow。当此 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;
                }                       

            }    
        });
    }    
}
4

5 回答 5

3

The popview when created takes away focus from the mainView so the user is not able to click the elements which are on the main view.
To click on the main view one need to dismiss popview first.
With respect to the above theory in your code You trying to dismiss popview by clicking on the button which is at main activity which is not possible.

Below code has the changes which you need to incorporate in your above code

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;
               // }                       

            }    
        });
}
}
于 2012-12-18T12:26:00.523 回答
0

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

于 2012-12-18T12:49:10.200 回答
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时,我可以访问单击父视图按钮..:)

于 2012-12-18T13:16:16.203 回答
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>
于 2012-12-18T13:24:26.363 回答
0

这个对我有用 set popupWindow.setFocusable(false);,背景和弹出窗口关闭将同时工作

于 2016-07-20T07:18:05.913 回答