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

时间:2014-01-06 20:31:46

标签: android eclipse button android-activity

我正在尝试这样做,以便当用户按下默认活动上的按钮时,它会打开一个新活动。

这就是我所拥有的。

按钮的代码:

android:onClick="openmenu"

“openmenu”方法的代码:

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

干杯!

1 个答案:

答案 0 :(得分:1)

您可以执行以下方法:

在xml布局中为按钮定义ID

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

现在在您的活动类

 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;

     }
  }

 }