Android program design logic analysis-user Event Response-Use of callback Functions

Source: Internet
Author: User

 

Note: this series of articles focuses on instance analysis and can be run directly after completing the steps.

 

Example: Add two integers and display the result.

 

1. Create an Android project.

 

2. Edit res/layout/main. xml. Design interface elements.

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

<EditText

Android: id = "@ + id/int1"

Android: layout_width = "100px"

Android: layout_height = "wrap_content"

Android: text = "0"

/>

 

<EditText

Android: id = "@ + id/int2"

Android: layout_width = "100px"

Android: layout_height = "wrap_content"

Android: text = "0"

/>

<Button

Android: id = "@ + id/submit"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "executed"

/>

<LinearLayout

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "The result is"

/>

<TextView

Android: id = "@ + id/result"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "0"

/>

</LinearLayout>

</LinearLayout>

3. Edit the source file in the src directory.

 

Package com. taisen. android;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. TextView;

 

Public class CallBackAddMethodActivity extends Activity {

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

Button B = (Button) findViewById (R. id. submit );

OnClickListener l = new OnClickListener (){

Public void onClick (View v ){

// TODO Auto-generated method stub

TextView int1 = (TextView) findViewById (R. id. int1 );

TextView int2 = (TextView) findViewById (R. id. int2 );

TextView result = (TextView) findViewById (R. id. result );

Int a, B;

A = Integer. parseInt (int1.getText (). toString ());

B = Integer. parseInt (int2.getText (). toString ());

Result. setText (String. valueOf (a + B ));

}

};

B. setOnClickListener (l );

}

}

 

The class that inherits the Activity completes the user-clicked event response.

 

OnClickListener is called to implement the unique onClick (View v) method ). Finally, use setOnClickListener () to map the object to the View.

 

From Yanyun's column

Related Article

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.