Android buttons and android buttons

Source: Internet
Author: User

Android buttons and android buttons

This document records four button-based click events.

First

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 1 ---------------------- Button bt1 = (Button) findViewById (R. id. bt1); bt1.setOnClickListener (new MyListener ();} class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first ");}}}

Second

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 2 ---------------------- Button bt2 = (Button) findViewById (R. id. bt2); bt2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the second type") ;}});} class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first ");}}}

The Code volume is large and cannot be reused.

Third

Public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ Type 3 ---------------------- Button bt3 = (Button) findViewById (R. id. bt3); bt3.setOnClickListener (this) ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the third type ");}}

Fourth

Add the following code to activity_main.xml:

<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "fourth" android: onClick = "youyiyi"/>

Add the following code to MainActivity. java:

Public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // ------------ type 4 ---------------------- // when a button is clicked, this method will be called public void youyiyi (View v) {// TODO Auto-generated method stub System. out. println ("this is the fourth type ");}}

The code structure is poor.

The complete code is as follows:

Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/bt1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "first"/> <Button android: id = "@ + id/bt2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Type 2"/> <Button android: id = "@ + id/bt3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "third"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "fourth" android: onClick = "youyiyi"/> </LinearLayout>

MainActivity. java

Package com. wuyudong. clickevent; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 1 ---------------------- Button bt1 = (Button) findViewById (R. id. bt1); bt1.setOnClickListener (new MyListener (); // ------------ type 1 ---------------------- // ------------ type 2 ---------------------- Button bt2 = (Button) findViewById (R. id. bt2); bt2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the second type") ;}}); // ------------ the second type ---------------------- // ------------ the third type -------------------- Button bt3 = (Button) findViewById (R. id. bt3); bt3.setOnClickListener (this); // ------------ class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first type") ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the third type");} // ------------ the fourth type -------------------- // when the button is clicked, this method will be called public void youyiyi (View v) {// TODO Auto-generated method stub System. out. println ("this is the fourth type ");}}

 

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.