如何从之前的活动中获取额外的内容来设置不同的按钮意图?

发布于 2024-12-25 19:45:59 字数 1509 浏览 6 评论 0原文

我想将额外的内容传递给我的 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 技术交流群。

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

发布评论

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

评论(2

挖个坑埋了你 2025-01-01 19:45:59

看来您已经走在正确的轨道上了。

您需要做的第一件事是确保您的 Button 在 xml android:id="@+id/my_button" 中有一个 id

然后在 setContent 逻辑之后获取对该按钮的引用:

Button myButton = (Button)findViewById(R.id.my_button);

您可以然后向该按钮添加一个 onClick 侦听器来处理用户点击:

myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Your switch logic should go here!
            }
        });

在 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:

Button myButton = (Button)findViewById(R.id.my_button);

You can then add a onClick listener to that button to handle user clicks:

myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Your switch logic should go here!
            }
        });

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.

云醉月微眠 2025-01-01 19:45:59

您可能需要这样的事情:

 int valueFromBundle = get value from bundle here...; 

 Button ipButton = (Button)findViewById(R.id.my_button);

 ipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               switch(chooser) { 
                case 0:
                Intent i = new Intent(fromActivity.class, toActivity.class);
                startActivity(i);
                break;
                case 1:
                Intent i = new Intent(fromActivity.class, toActivity.class);
                startActivity(i);
                break;
                etc..,



             }

            }
        });

Something like this you may need to be:

 int valueFromBundle = get value from bundle here...; 

 Button ipButton = (Button)findViewById(R.id.my_button);

 ipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               switch(chooser) { 
                case 0:
                Intent i = new Intent(fromActivity.class, toActivity.class);
                startActivity(i);
                break;
                case 1:
                Intent i = new Intent(fromActivity.class, toActivity.class);
                startActivity(i);
                break;
                etc..,



             }

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