Android的Eclipse是如何启动的按钮,点击一个新的活动按钮、Android、Eclipse

2023-09-12 06:09:56 作者:该死的心动

我试图让这个当用户presses上的默认行为的一个按钮,它会打开一个新的活动。

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

下面就是我的本钱。

在$ C $下按钮:

android:onClick="openmenu"

在code为openmenu的方法:

The code for the "openmenu" method:

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

干杯家伙!

推荐答案

您可以做的解决方法,如:

You can do workaround like :

定义一个ID为你的XML布局按钮

Define an Id for your button in xml layout

 <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;

     }
  }

 }