将值从活动传递到弹出窗口

时间:2013-01-29 11:25:50

标签: android popup

如何将值从活动传递到弹出窗口?虽然这似乎是一个愚蠢的问题。代码段如下所示。我需要将函数onButtonPopup()中的值传递给弹出窗口(shape_details.xml)。提前谢谢。

public void onButtonPopup(View target, int position) {
    // Make a View from our XML file
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.shape_details, (ViewGroup) findViewById(R.id.llShapeDetails));

    Display display = getWindowManager().getDefaultDisplay();




    int width = display.getWidth();
    int height = display.getHeight();

    pw = new PopupWindow(layout, width - width / 4, height - height / 3, true);

    pw.setAnimationStyle(R.style.PopupWindowAnimation);
    pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}

1 个答案:

答案 0 :(得分:0)

要发送/传递任何活动的任何值,我们需要两个活动。像第一个是“ MainActivity”,第二个是“ PopUpWindowActivity”。现在,我们创建“ PopUpWindowActivity”。

  public class PopUpWindowActivity {
  public void onButtonPopup(final View target, int position) {
    //You can work here with "position"
    }
  }

现在,我们将从“ MainActivity”发送/传递值

    public class MainActivity extends AppCompatActivity {
           //onCreate Metod and other work here
           //Here we will send/pass data to "PopUpWindowActivity"
           SampleButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PopUpWindowActivity popUpWindow = new PopUpWindowActivity();
                    popUpWindow.onButtonPopup(view, 1);
                    //I take position value is 1. You can send your own.
                }
            });
    }