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

作成されたポップビューは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;
               // }                       

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

フォーカス可能をtrueに設定して、親ビューをブロックします。したがって、falseに設定すると、親ビューボタンをクリックするためのアクセス権を取得しました。.:)

于 2012-12-18T13:16:16.203 に答える
0

もう1つの最も簡単な方法は

    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

これは私のために働いていますsetpopupWindow.setFocusable(false);、backgroundとpopwindowdismissは同時に動作します

于 2016-07-20T07:18:05.913 に答える