Android eclipse 如何在单击按钮时启动新活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20958724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:27:51  来源:igfitidea点击:

Android eclipse how to start a new activity on button click

androideclipsebuttonandroid-activity

提问by user3166793

I am trying to make it so that when the user presses a button on the default activity, it opens a new activity.

我试图做到这一点,当用户按下默认活动上的按钮时,它会打开一个新活动。

Here's what I've got.

这就是我所拥有的。

The code for the button:

按钮的代码:

android:onClick="openmenu"

The code for the "openmenu" method:

“openmenu”方法的代码:

public void openmenu(View view) {
    Intent intent = new Intent(this , MainMenuPassed.class);
    startActivity(intent);
} 

Cheers guys!

干杯伙计们!

回答by pyus13

You can do workaround like :

您可以执行以下解决方法:

Define an Id for your button in xml layout

在 xml 布局中为您的按钮定义一个 ID

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Text"
  />

Now in your Activity Class

现在在你的活动课上

 public class MyActivity extends Activity implements View.OnClickListener{



   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    final Button button =(Button) findVieById(R.id.button);
    button.setOnClickListener(this);

   }

   @Override
   protected void onClick(View view) {

     switch(v.getId){
       case R.id.button :
          Intent intent = new Intent(this , MainMenuPassed.class);
          startActivity(intent);
       break;

     }
  }

 }