2012-12-18 37 views
1

在我的父級活動我有一個按鈕,當我點擊它顯示PopUpWindow與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; 
       }      

      }  
     }); 
    }  
} 

回答

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

      }  
     }); 
} 
} 
+0

只需使用popupWindow.setFocusable(false); ,popwindow解僱和背景視圖點擊將同時工作。 –

0

爲了使活動按鈕合併更改,必須關閉彈出窗口。

0

感謝答案,對不起,回答我的問題。在這一行

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

這意味着...

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

我可調焦設置爲true,所以其阻斷父視圖。所以當我把它設置爲假我有權訪問單擊父視圖按鈕.. :)

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> 
0

這一個是爲我工作組popupWindow.setFocusable(假);,背景和popwindow解僱將努力同時間