找到调用意图/活动的按钮的 id?安卓

发布于 2024-12-26 13:11:22 字数 307 浏览 1 评论 0原文

我的主要活动布局包含 10 个不同的按钮,它们都调用同一个活动 (MapsActivity)。

如何找到从 MapsActivity 按下的按钮的 id?我尝试使用发送它

intent.putExtra("id", id);

但是当按下任何按钮时,这个 id 的结果都是一样的。

我是否需要为所有调用相同活动的每个按钮创建一个单独的意图?或者我可以使用 1 个意图并仅更改它传递给 MapsActivity 的值吗?

我希望我已经足够清楚地解释了这个问题!谢谢!

My main activity layout contains 10 different buttons that all call the same activity (MapsActivity).

How can I find out the id of the button that was pressed from MapsActivity ? I tried to send it by using

intent.putExtra("id", id);

But this id came out as the same when pushing any of the buttons.

Do I need to create a separate intent for each button that all call the same activity? Or can I use 1 intent and just change what value it passes to MapsActivity?

I hope I've explained this problem clearly enough! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-01-02 13:11:22

您说您使用 findViewById 将侦听器添加到代码中的按钮,所以我假设您有要求,这意味着您无法在 xml 中设计布局。因此,当您设置第一个活动(包含所有这些按钮的活动)时,请确保它们都获得相同的单击侦听器。我们可以调用这个监听器方法mapsButtonListener。它应该这样声明:

public void mapsButtonListener(View sender) {
    int id = sender.getId();
    Intent mapsActivity = new Intent(getApplicationContext(), MapsActivity.class);
    mapsActivity.putExtra("button_id", id);
    startActivity(mapsActivity);
}

关于我们按钮的 id:s

如果您使用 xml 设计放置按钮,那么只需确保它们在 xml 中声明了 id:s 即可。但是,如果您没有使用 xml 来实现这些,而是​​在代码中实例化它们,那么您必须使用方法 setId(4) 来给您的按钮 id:s (显然不要给所有按钮 4,呵呵)。这应该与添加点击监听器同时完成。另外,请确保您知道哪个按钮获得哪个数字,以便您稍后可以检查。

在MapsActivity中获取id

一旦mapsActiviy启动,我们就会需要该id。我们使用以下行(在 MapsActivity 类内)执行此操作:

 int id = getIntent().getIntExtra("button_id");

you say you add the listeners to the buttons in your code with findViewById, so i'm assuming you have requirements which mean that you can't design your layout in xml. so, when you set up your first activity, the one that holds all these buttons, make sure they all get the same click listener. we can call this listener method mapsButtonListener. it should be declared like this:

public void mapsButtonListener(View sender) {
    int id = sender.getId();
    Intent mapsActivity = new Intent(getApplicationContext(), MapsActivity.class);
    mapsActivity.putExtra("button_id", id);
    startActivity(mapsActivity);
}

about our button's id:s

if you're placing your buttons with xml design, then just make sure they have id:s declared in the xml. however, if you're not using xml for these, but instead instantiating them in the code, then you'll have to use the method setId(4) to give your buttons id:s (don't give all of them 4 obviously, hehe). this should be done at the same time as adding the click listener. also, make sure you know what button gets what number, so that you can check this later.

getting the id in MapsActivity

once mapsActiviy starts, we'll want that id. we do this with the following line (inside the MapsActivity class):

 int id = getIntent().getIntExtra("button_id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文