0

如何将活动中的值传递给弹出窗口?虽然这似乎是一个愚蠢的问题。代码片段如下所示。我只需要将函数 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);
}
4

1 回答 1

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.
                }
            });
    }
于 2020-07-09T17:07:29.417 回答