[Android] controls and basic event response methods

Source: Internet
Author: User

The first method is [java] package com. app; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Intent intent = new Intent (MainActivity. this, SecondActivity. class); startActivity (intent) ;}}}} method 2 [java] package com. app; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class SecondActivity extends Activity implements OnClickListener {private Button button1; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_welcome); button1 = (Button) findViewById (R. id. button1); button1.setOnClickListener (this) ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. button1: Intent intent = new Intent (); intent. setClass (SecondActivity. this, ThirdActivity. class); startActivity (intent); break; default: break ;}} method 3 [java] package com. app; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class ThirdActivity extends Activity {private Button_OnClick mbutton = new Button_OnClick (); @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_third); Button button = (Button) findViewById (R. id. button1); button. setOnClickListener (mbutton);} class Button_OnClick implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. button1: Intent intent = new Intent (); intent. setClass (ThirdActivity. this, MainActivity. class); startActivity (intent); break; default: break ;}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.