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