Android Custom Controls

Source: Internet
Author: User

Study reference: http://blog.csdn.net/hudashi/article/details/50913257

http://blog.csdn.net/gebitan505/article/details/18264091

http://blog.csdn.net/psuaije/article/details/8662266

Http://www.cnblogs.com/yc-755909659/archive/2015/02/10/4283294.html


Here is the source code: an interface is added to the code to set custom events for custom controls

MyControl. Java code:

[Java]View Plaincopy
  1. Package Paj.control;
  2. Import Android.content.Context;
  3. Import Android.util.AttributeSet;
  4. Import Android.view.View;
  5. Import Android.widget.Button;
  6. Import Android.widget.EditText;
  7. Import Android.widget.LinearLayout;
  8. Inherit LinearLayout
  9. Public Class MyControl extends LinearLayout {
  10. /** 
  11. * Must have an interface
  12. */
  13. public interface icoallback{
  14. public void OnClickButton (String s);
  15. }
  16. /** 
  17. * Initialize interface variables
  18. */
  19. Icoallback icallback = null;
  20. /** 
  21. * Custom Control Custom Events
  22. * @param Iback interface type
  23. */
  24. public void Setonclick (Icoallback iback)
  25. {
  26. Icallback = Iback;
  27. }
  28. //////////////////////////////////////////////////////////////////////////////  
  29. ////////////////////////////////////////////////////////////////////////////  
  30. private Context _context;
  31. /** 
  32. * Two parameters of the constructor (must use the two parameter constructor, because the custom control needs to use in the XML layout, contains the property)
  33. * @param context invokes the object of the custom control
  34. * @param attrs
  35. */
  36. Public mycontrol (context context, AttributeSet attrs) {
  37. Super (context, attrs);
  38. _context = Context;
  39. //Add a custom control to the main layout
  40. This.addview (Createlayout ());
  41. }
  42. private View Createlayout () {
  43. //Create a lainearlayout layout
  44. LinearLayout layout = new LinearLayout (_context);
  45. Linearlayout.layoutparams params = new Linearlayout.layoutparams (Layoutparams.fill_parent, LAYOUTPARAMS.WRAP_  CONTENT);
  46. Layout.setorientation (linearlayout.vertical);
  47. Layout.setlayoutparams (params);
  48. //Create a text edit box
  49. final EditText edit = new EditText (_context);
  50. Layoutparams editparams = new Layoutparams (Layoutparams.fill_parent, layoutparams.wrap_content);
  51. Edit.setlayoutparams (Editparams);
  52. //Create a button
  53. Button button = New button (_context);
  54. Layoutparams btpparams = new Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content);
  55. Button.setlayoutparams (Btpparams);
  56. Button.settext ("Click to get");
  57. //Set button's Click event
  58. Button.setonclicklistener (new Onclicklistener () {
  59. @Override
  60. public void OnClick (View v) {
  61. //Returns the calculated value in this custom control, using the callback implementation
  62. Icallback.onclickbutton (Edit.gettext (). toString ());
  63. }
  64. });
  65. //Text edit boxes and buttons added to layout layouts
  66. Layout.addview (edit);
  67. Layout.addview (button);
  68. return layout;
  69. }
  70. }



Activity_main.xml Code

[HTML]View Plaincopy
  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. <!--define your own controls--
  3. xmlns:paj_control="Http://schemas.android.com/apk/res/paj.control.mycontrol"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. >
  8. <!--custom Controls --
  9. <paj.control.mycontrol android:id="@+id/mycontrol"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. />
  13. <!--Displays the value returned by the custom control-
  14. <TextView android:id="@+id/test"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. />
  18. </linearlayout>


Mainactivity.java Code:

[Java]View Plaincopy
    1. Package com.example.controlstest;
    2. Import Paj.control.mycontrol;
    3. Import Paj.control.mycontrol.ICoallBack;
    4. Import Android.os.Bundle;
    5. Import android.app.Activity;
    6. Import Android.view.Menu;
    7. Import Android.widget.TextView;
    8. Public class Mainactivity extends Activity {
    9. MyControl _mmycontrol;
    10. TextView TextView;
    11. @Override
    12. protected void OnCreate (Bundle savedinstancestate) {
    13. super.oncreate (savedinstancestate);
    14. Setcontentview (R.layout.activity_main);
    15. TextView = (TextView) Findviewbyid (r.id.test);
    16. //Get custom Controls
    17. _mmycontrol = (mycontrol) Findviewbyid (R.id.mycontrol);
    18. //Implement Setonclick custom events in custom controls
    19. _mmycontrol.setonclick (new Icoallback () {
    20. @Override
    21. public void OnClickButton (String s) {
    22. Textview.settext (s); //The value passed by the custom control is displayed in the text box
    23. }
    24. });
    25. }
    26. }

Android Custom Controls

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.