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

标签 android android-activity switching

我有 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/

相关文章:

android - manifestPlaceHolders 不替换值

android - 如何在 multiautocompletetextview 中使用 gmail 等空间标记器在 android 中正确添加和删除联系人气泡

python - 根据 txt 文件的搜索结果分配变量值

java - Jetty - 对不同的应用程序版本使用符号链接(symbolic link)

android - 如何获取图片资源

android - flutter:应用关闭时通过接收短信弹出通知

java - 我们如何保证 onAttach 始终有效(Fragment)?

android - 启动 Intent.ACTION_SEND 总是返回 resultCode 0

android - 单个 Activity 应用程序的多个快捷方式

android - 将 View 或 Activity 交换为布局的一部分?