I. Key Points
How to use custom controls in layout (XML)
Ii. Example
1. function: implement a new browser control so that log information can be printed anywhere in the browser.
2. steps:
1) create a project
A) In eclipse, choose File> New> project ......
B) Select Android project and press next
C) Fill in the project content as follows:
Project name: test_xy // directory name, which is located under the workspace You Set
Package name: COM. Android. Test // package name
Activity name:. testxy // Class Name (generated file testxy. Java)
Application name: test_xy // name of the executable program
Click Finish.
2) inherit an existing control and add new attributes and Methods
A) on the left side of Eclipse: test_xy-> Src-> com. Android. Test, right-click New-> class.
B) create a new control: Name: mywebview. Other Default options are used.
The content of mywebview. Java is as follows:
Package COM. android. test; <br/> Import android. view. motionevent; <br/> Import android. webKit. webview; <br/> Import android. content. context; <br/> Import android. util. attributeset; <br/> Import android. util. log; <br/> public class mywebview extends webview {<br/> Public mywebview (context) {<br/> This (context, null ); <br/>}< br/> Public mywebview (context, attributeset attrs) {<br/> This (context, attrs, 0 ); <br/>}< br/> Public mywebview (context, attributeset attrs, int defstyle) {<br/> super (context, attrs, defstyle ); <br/>}// note that the constructor with three parameters is implemented. <br/> Public Boolean ontouchevent (motionevent eV) {// Add new function <br/> int action = eV. getaction (); <br/> log. D ("xy_test", "Now Recv key:" + action); <br/> return Super. ontouchevent (EV); <br/>}< br/>}
3) modify the XML file
A) modify the content in test_xy-> res-> Layout-> main. xml on the left side of eclipse as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = http://schemas.android.com/apk/res/android <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello"/> <br/> <COM. android. test. mywebview <br/> Android: Id = "@ + ID/myview" Android: layout_height = "fill_parent" <br/> Android: layout_width = "fill_parent"/> <br/> </linearlayout>
Note that the full name is com. Android. Test. mywebview. Otherwise, the new control cannot be found.
4) Run
A) In eclipse, click Run> RUN configurations ......
B) double-click the Android Application on the left to generate a new configuration. Click it and fill in the following content:
Name: yan_config // any one
Project: test_xy // The project just started, that is, the directory name
C) Click "Apply", click "run", and wait for a while.
D) Click "ddms" in the upper right corner to view the log information. When you touch the webview control, you can see the log information you just added.