2012-12-18 1 views
1

Dans mon activité parent j'ai un bouton, quand je clique dessus, il affiche PopUpWindow avec 2 ImageButton .. Lorsque ce PopUpWindow existe, je ne peux pas cliquer sur mon bouton d'activité parent .. Voici mon code, est-il un problème dans ce ..Impossible de cliquer sur le bouton d'activité parent lorsque PopupWindow montrant

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

      }  
     }); 
    }  
} 

Répondre

3

le popView lors de sa création enlève le focus de la MainView afin que l'utilisateur ne peut pas cliquer sur les éléments qui sont sur la vue principale.
Pour cliquer sur la vue principale, vous devez d'abord ignorer popview.
En ce qui concerne la théorie ci-dessus dans votre code Vous essayez de rejeter popview en cliquant sur le bouton qui est à l'activité principale qui n'est pas possible.

Ci-dessous le code a les changements que vous devez intégrer dans votre code ci-dessus

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

Utilisez simplement popupWindow.setFocusable (false); , popwindow rejeter et vue en arrière-plan cliquer avec fonctionnera en même temps. –

0

Pour activer le bouton d'activité, vous devez fermer la fenêtre pop-up.

0

Merci pour les réponses, Désolé pour répondre à ma question .. Dans cette ligne

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

cela signifie ...

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

i jeu focusable comme vrai si sa vue parent blocage .. Donc quand je l'ai mis à false i eu accès à cliquer sur le bouton parentview .. :)

0

Une autre façon la plus simple est

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

Celui-ci travaille pour moi mettre popupWindow.setFocusable (false) ;, fond et popwindow rejeter fonctionnera même time

Questions connexes