Three ways to do the OnClick event of Android Development (RPM)

Source: Internet
Author: User

1  Packagea.a;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.widget.Button;7 ImportAndroid.widget.EditText;8 9  Public classAactivityextendsActivity {Ten     /**Called when the activity is first created.*/ One  A EditText Ev1; -  - @Override the      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); -  +EV1 =(EditText) Findviewbyid (R.ID.EDITTEXT1);  -  +         //The first way AButton Btn1 = (button) Findviewbyid (R.id.button1);//Get button Resource atBtn1.setonclicklistener (NewButton.onclicklistener () {//Create a listener -              Public voidOnClick (View v) { -String strtmp = "click Button01";  - Ev1.settext (strtmp);  -             }   -  in         });  -  to         //The second way +Button Btn2 = (button) Findviewbyid (R.id.button2);//Get button Resource -Btn2.setonclicklistener (listener);//Setting up monitoring the  *     } $ Panax NotoginsengButton.onclicklistener listener =NewButton.onclicklistener () {//Creating Listener Objects -          Public voidOnClick (View v) { theString strtmp= "click Button02";  + Ev1.settext (strtmp);  A         }   the  +     }; -  $  $     //The Third Way (available in the Android1.6 version and later versions) -      Public voidBtn3onclick (view view) { -String strtmp= "click Button03"; the Ev1.settext (strtmp); - Wuyi     }   the}
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7     <TextView8         Android:layout_width= "Fill_parent"9 Android:layout_height= "Wrap_content"Ten Android:text= "@string/hello" /> One  A     <Button -         Android:id= "@+id/button1" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" - Android:text= "Button1" /> -  -     <Button +         Android:id= "@+id/button2" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" A Android:text= "Button2" /> at      -  <Button -         Android:id= "@+id/button3" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Button3"  in Android:onclick= "Btn3onclick"/> -  to  <EditText +      Android:id= "@+id/edittext1" - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" > *  $      <Requestfocus/>Panax Notoginseng  </EditText> -  the </LinearLayout>

Three ways to do the OnClick event of Android Development (RPM)

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.