android eclipse button OnClick event

前端 未结 6 1639
别跟我提以往
别跟我提以往 2020-12-18 08:03

I have 2 files: main_activity.xml and home.xml. I made a button in main_activity.xml

Here is the code snippet:



        
6条回答
  •  情歌与酒
    2020-12-18 08:23

    For managing click activity in android ,you can do as below

    1. Implement OnClickListener on YourActivity.java class like

      public class MainActivity extends Activity implements OnClickListener

    2. Then, declare your button in .java class like

      Button btn = (Button) findViewById(R.id.btnPlay);

    3. Then use button btn variable as below

      btn.setOnClickListener(new View.OnClickListener() {
      
          public void onClick(View v) {
              myClick(v); /* my method to call new intent or activity */
          }
      });
      
    4. Handle the click event:

      public void myClick(View v) {
          Intent intent = new Intent(**this, Swipe.class**);
          startActivity(intent);// for calling the activity
      }
      

    you also need to register your activity(.java) in android manifest as below

    
    
    

提交回复
热议问题