Android 2弹出窗口,一个按钮打开两个

时间:2014-06-29 19:11:56

标签: android layout popup

我在操作栏上有两个按钮,它们都应该打开弹出窗口,另一个弹出窗口 -

第一家店。

秒 - 暂停菜单。

这是按钮代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.PAUSE:
            openPopUP();
        case R.id.SHOP:
            openSHOP();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
private void openSHOP() {
    showPopup1(FirstActivity.this, p);  

}
private void openPopUP() {
    showPopup(FirstActivity.this, p);   
}

这是弹出式代码:

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
    int[] location = new int[2];
    p = new Point();
    p.x = location[0];
    p.y = location[1];
    }
 private void showPopup(final Activity context, Point p) { 
    int popupWidth = 2000;
    int popupHeight = 2000;
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) context
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);
    int OFFSET_X = 60;
    int OFFSET_Y = 60;
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    Button close = (Button) layout.findViewById(R.id.close);
    Button restart = (Button) layout.findViewById(R.id.restart);
    final Button music = (Button) layout.findViewById(R.id.music);
    musicstop = (Button) layout.findViewById(R.id.musicstop);
    musicstop.setVisibility(View.GONE);
    music.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
              mPlayer.pause(); 
              musicstop.setVisibility(View.VISIBLE);
              music.setVisibility(View.GONE);}  });
    musicstop.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {       
              musicstop.setVisibility(View.GONE);
              music.setVisibility(View.VISIBLE);
                mPlayer.start(); }  });
    close.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        popup.dismiss(); 
        }  });
    restart.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            popup.dismiss();
            highscore_int.setText("");
            YouLost.setText("Game restarted"); 
            counter=0;
            TimeCounter=0;
            TimeCounter1=0;
            TimeCounter2=0;
            TimeCounter3=0;
            TimeCounter4=0;
            TimeCounter5=0;
            TimeCounter6=0;
            TimeCounter7=0;
            TimeCounter8=0;
            TimeCounter9=0;
            TimeCounter10=0;
            TimeCounter11=0;
            TimeCounter12=0;
            TimeCounter13=0;
            TimeCounter14=0;
            TimeCounter15=0;
            TimeCounter16=0;
            changingTextView.setText(""+counter);
            button11.setBackgroundColor(Color.rgb(TimeCounter, TimeCounter, TimeCounter));  
            button10.setBackgroundColor(Color.rgb(TimeCounter1*2, TimeCounter1*2, TimeCounter1*2)); 
            button9.setBackgroundColor(Color.rgb(TimeCounter2*3, TimeCounter2*3, TimeCounter2*3));  
            button8.setBackgroundColor(Color.rgb(TimeCounter3*4, TimeCounter3*4, TimeCounter3*4));  
            button7.setBackgroundColor(Color.rgb(TimeCounter4*5, TimeCounter4*5, TimeCounter4*5));  
            button6.setBackgroundColor(Color.rgb(TimeCounter5*6, TimeCounter5*6, TimeCounter5*6));  
            button5.setBackgroundColor(Color.rgb(TimeCounter6*7, TimeCounter6*7, TimeCounter6*7));  
            button3.setBackgroundColor(Color.rgb(TimeCounter8*9, TimeCounter8*9, TimeCounter8*9));  
            button1.setBackgroundColor(Color.rgb(TimeCounter9*10, TimeCounter9*10, TimeCounter9*10));   
            button13.setBackgroundColor(Color.rgb(TimeCounter10*11, TimeCounter10*11, TimeCounter10*11));   
            button14.setBackgroundColor(Color.rgb(TimeCounter11*12, TimeCounter11*12, TimeCounter11*12));   
            button15.setBackgroundColor(Color.rgb(TimeCounter12*13, TimeCounter12*13, TimeCounter12*13));   
            button16.setBackgroundColor(Color.rgb(TimeCounter13*14, TimeCounter13*14, TimeCounter13*14));   
            button17.setBackgroundColor(Color.rgb(TimeCounter14*15, TimeCounter14*15, TimeCounter14*15));   
            mPlayer.start();
            } });
    }
 private void showPopup1(final Activity context, Point p) { 
     int popupWidth = 2000;
     int popupHeight = 2000;
     LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
     LayoutInflater layoutInflater = (LayoutInflater) context
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View layout = layoutInflater.inflate(R.layout.shoplayout, viewGroup);
     final PopupWindow popup = new PopupWindow(context);
     popup.setContentView(layout);
     popup.setWidth(popupWidth);
     popup.setHeight(popupHeight);
     popup.setFocusable(true);
     int OFFSET_X = 60;
     int OFFSET_Y = 60;
     popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
     Button button1 = (Button) layout.findViewById(R.id.button1);
     Button button2 = (Button) layout.findViewById(R.id.button2);
     Button button3 = (Button) layout.findViewById(R.id.button3);
     Button button4 = (Button) layout.findViewById(R.id.button4);
     Button button5 = (Button) layout.findViewById(R.id.button5);
     Button button6 = (Button) layout.findViewById(R.id.button6);
     button1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });
     button2.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });
     button3.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });
     button4.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });
     button5.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });
     button6.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             popup.dismiss();
            }  });

 }

我的问题是,商店弹出窗口是正确的,打开正确的按钮,一切都很好,但当我点击暂停按钮,它打开商店,当我退出商店时,它显示我暂停菜单。基本上,暂停按钮打开2个弹出窗口。为什么呢?

1 个答案:

答案 0 :(得分:0)

每个有这个问题的人,我解决了,你只需要在每个案例后添加这一行:

return true;

我忘了在一个案例之后添加它,因为这样做了两个案例:)