如何在Android中切换beteen多个活动

kit*_*tes 2 android switching android-activity

我有8个Screenns.我准备了8个活动.在第一个活动中,我已经给出了这个代码,从Ist Activity切换到IInd On Image按钮给出On Click

public void onClick(View v) { 
Intent myIntent = new Intent(v.getContext(), Activity2.class);
     v.getContext().startActivity(myIntent);
});
Run Code Online (Sandbox Code Playgroud) 如何将第二个活动切换到第三个活动,第三个活动切换到第四个活动,依此类推.

请帮助我.

Kei*_*ith 7

这是你可以在下面做的一种方式.在此示例中,您将在屏幕上放置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;   

       } 
    } 
};
   }
Run Code Online (Sandbox Code Playgroud)

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

然后我们设置一个处理程序,它监听我的按钮上的点击.接下来,需要知道按下了哪个按钮.开关/盒子就像"if then".如果他们按下按钮b1,代码将我们带到我们分配给该按钮单击的内容.按b1(按钮1),我们转到我们分配给它的"意图"或活动.

希望这有点帮助如果有任何用处,请不要忘记投票.我自己刚刚开始研究这些东西.

谢谢,