如何从之前的活动中获取额外的内容来设置不同的按钮意图?
我想将额外的内容传递给我的 NextActivity,以便 NextActivity 中的按钮可以具有不同的意图。我成功地执行此操作以查看不同的布局,但不知道如何在按钮上执行此操作。 这是具有工作 setContentView 开关的代码;
public class ContentViewer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
int chooser = bundle.getInt("Layout");
switch(chooser) {
case 0:
setContentView(R.layout.about);
break;
case 1:
setContentView(R.layout.contact);
break;
case 2:
setContentView(R.layout.contentviewer);
break;
case 3:
setContentView(R.layout.contact);
break;
case 4:
setContentView(R.layout.contact);
break;
case 5:
setContentView(R.layout.contact);
break;
case 6:
setContentView(R.layout.contact);
break;
case 7:
setContentView(R.layout.contact);
break;
case 8:
setContentView(R.layout.contact);
break;
case 9:
setContentView(R.layout.contact);
break;
}
}
}
现在,在这些布局中,有一个具有相同 ID 的按钮,但我希望它根据不同的情况具有不同的 Intents(如上面的 setContentView)。
更新 有MainActivity,它有一个传递额外信息的列表视图。当单击列表视图中的某个项目时,它将打开 NextActivity(如代码中所示)。 NextActivity 有一个带有按钮的布局。现在,根据在 MainActivity 上单击的项目,按钮将具有不同的意图。例如,如果在 MainActivity 中单击了项目 1,则打开 NextActivity,覆盖 Button 以具有意图 1。如果在 MainActivity 中单击项目 2,则使用覆盖按钮打开 NextActivity 以具有意图 2,而不是意图 1。足够清楚吗?
I want to pass extra to my NextActivity so that a button in NextActivity can have different intents. I am successful with doing this to view different layouts, but no clue on how to do this on a button.
Heres the code that has a working setContentView switch;
public class ContentViewer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
int chooser = bundle.getInt("Layout");
switch(chooser) {
case 0:
setContentView(R.layout.about);
break;
case 1:
setContentView(R.layout.contact);
break;
case 2:
setContentView(R.layout.contentviewer);
break;
case 3:
setContentView(R.layout.contact);
break;
case 4:
setContentView(R.layout.contact);
break;
case 5:
setContentView(R.layout.contact);
break;
case 6:
setContentView(R.layout.contact);
break;
case 7:
setContentView(R.layout.contact);
break;
case 8:
setContentView(R.layout.contact);
break;
case 9:
setContentView(R.layout.contact);
break;
}
}
}
Now, in these layouts, there is a button with the same ID, but i want it to have different Intents based on different cases(like the setContentView above).
UPDATE
Theres MainActivity, it has a listview that passes extra. When an item in the listview is clicked, it will open the NextActivity(like in code). NextActivity has a layout that has a Button. Now, based on which item was clicked on the MainActivity, the Button will have different Intents. For example, if in MainActivity, item 1 was clicked, open NextActivity, override Button to have intent 1. If in MainActivity item 2 was clicked, open NextActivity with override button to have intent 2 INSTEAD of intent 1. Clear enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您已经走在正确的轨道上了。
您需要做的第一件事是确保您的 Button 在 xml
android:id="@+id/my_button"
中有一个 id然后在 setContent 逻辑之后获取对该按钮的引用:
您可以然后向该按钮添加一个 onClick 侦听器来处理用户点击:
在 onClick 函数中,您可以添加切换逻辑,您可能有一个单独的额外功能,但这取决于您,您基本上可以执行与上面相同的操作,但是调用不同的意图而不是设置内容。
Looks like you are already on the right track.
The first thing you will need to do is make sure your Button has an id in xml
android:id="@+id/my_button"
Then after your setContent logic get a reference to that button:
You can then add a onClick listener to that button to handle user clicks:
In the onClick function you can add your switch logic, you might have a separate extra for this but that is up to you, you can essentially do the same as you did above but call a different intent instead of set the content.
您可能需要这样的事情:
Something like this you may need to be: