如何在Android中的多个活动之间切换
我有8个屏幕。我为此准备了8个活动。 在第一个活动中,我给出了这个代码 从Ist活动切换到IInd 在图像按钮上单击如何在Android中的多个活动之间切换,android,android-activity,switching,Android,Android Activity,Switching,我有8个屏幕。我为此准备了8个活动。 在第一个活动中,我给出了这个代码 从Ist活动切换到IInd 在图像按钮上单击 public void onClick(View v) { Intent myIntent = new Intent(v.getContext(), Activity2.class); v.getContext().startActivity(myIntent); }); 如何将第二个活动切换到第三个活动, 第三个活动到第四个活动,依此类推 请在这方面帮助我。让我们尝
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Activity2.class);
v.getContext().startActivity(myIntent);
});
如何将第二个活动切换到第三个活动,
第三个活动到第四个活动,依此类推
请在这方面帮助我。让我们尝试使用下面url中的代码片段,并浏览《开发者指南》中的标志
下面是一种方法。在本例中,您在屏幕上放置了3个按钮。这些是我在XML文件中定义和布置的按钮。单击3个不同按钮中的任意一个,它将带您进入相应的活动
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;
}
}
};
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//下面是抓取和布局按钮的代码,它们被命名为b1、b2等,并以此标识。
按钮b1=(按钮)findViewById(R.id.b1);
按钮b2=(按钮)findViewById(R.id.b2);
按钮b3=(按钮)findViewById(R.id.b3);
//设置按钮的侦听器和按钮处理程序
b1.设置ClickListener(buttonhandler);
b2.setOnClickListener(buttonhandler);
b3.设置ClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=新建视图。OnClickListener(){
//现在我需要确定单击了哪个按钮,以及要启动哪个意图或活动。
公共void onClick(视图v){
开关(v.getId()){
//现在,他们按了哪个按钮,带我去了那个课堂/活动
case R.id.b1://为什么不在每个活动中编写相同的代码/onClick(参数为后续参数)?这看起来像是可怕的代码。您是否阅读了关于如何切换开始活动的单个教程?