gpt4 book ai didi

android - 如何在 Android 中的多个 Activity 之间切换

转载 作者:太空狗 更新时间:2023-10-29 16:10:15 25 4
gpt4 key购买 nike

我有 8 个屏幕。我为此准备了 8 个 Activity 。在第一个 Activity 中,我给出了这段代码从 Ist Activity 切换到 IInd在图像按钮上点击

public void onClick(View v) { 
Intent myIntent = new Intent(v.getContext(), Activity2.class);
v.getContext().startActivity(myIntent);
});
如何将第二个 Activity 切换到第三个 Activity ,第三个 Activity 到第四个 Activity ,依此类推。

请帮助我。

最佳答案

下面是您可以执行此操作的一种方法。在此示例中,您将在屏幕上放置 3 个按钮。这些是我在 XML 文件中定义和布局的按钮。单击 3 个不同按钮中的任意一个,即可转到相应的 Activity 。

    public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button b1 =(Button)findViewById(R.id.b1);
Button b2 =(Button)findViewById(R.id.b2);
Button b3 =(Button)findViewById(R.id.b3);

// Setup the listeners for the buttons, and the button handler
b1.setOnClickListener(buttonhandler);
b2.setOnClickListener(buttonhandler);
b3.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {

// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {

// Now, which button did they press, and take me to that class/activity

case R.id.b1: //<<---- notice end line with colon, not a semicolon
Intent myIntent1 = new Intent(yourAppNamehere.this, theNextActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent1);
break;

case R.id.b2: //<<---- notice end line with colon, not a semicolon
Intent myIntent2 = new Intent(yourMainAppNamehere.this, AnotherActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent2);
break;

case R.id.b3:
Intent myIntent3 = new Intent(yourMainAppNamehere.this, a3rdActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent3);
break;

}
}
};
}

基本上,我们正在做几件事来设置它。识别按钮并将它们从 XML 布局中拉入。查看如何为每个分配一个 id 名称。例如,r.id.b1 是我的第一个按钮。

然后我们设置了一个处理程序,它监听对我的按钮的点击。接下来,需要知道按下了哪个按钮。 switch/case 就像一个“if then”。如果他们按下按钮 b1,代码会将我们带到我们分配给该按钮点击的内容。按 b1(按钮 1),然后我们转到我们分配给它的“Intent ”或 Activity 。

希望这对您有所帮助如果有任何用处,请不要忘记对答案进行投票。我自己才刚刚开始研究这些东西。

谢谢,

关于android - 如何在 Android 中的多个 Activity 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3855729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com