android 自訂控制項

來源:互聯網
上載者:User

標籤:

學習參考: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


下面是原始碼:代碼中添加了一個介面,這個介面用於給自訂控制項設定自訂的事件

mycontrol.Java代碼:

 

[java] view plaincopy 
  1. package paj.control;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.LinearLayout;  
  9. //繼承LinearLayout  
  10. public class mycontrol extends LinearLayout {  
  11.   
  12.     /** 
  13.      * 一定一個介面 
  14.      */  
  15.     public interface ICoallBack{  
  16.         public void onClickButton(String s);  
  17.     }  
  18.       
  19.     /** 
  20.      * 初始化介面變數 
  21.      */  
  22.     ICoallBack icallBack = null;  
  23.       
  24.     /** 
  25.      * 自訂控制項的自訂事件 
  26.      * @param iBack 介面類型 
  27.      */  
  28.     public void setonClick(ICoallBack iBack)  
  29.     {  
  30.         icallBack = iBack;  
  31.     }  
  32.       
  33.     //////////////////////////////////////////////////////////////////////////////  
  34.     ////////////////////////////////////////////////////////////////////////////  
  35.       
  36.     private Context _Context;  
  37.       
  38.     /** 
  39.      * 兩個參數的建構函式(必須使用兩個參數的建構函式,因為自訂的控制項需要在XML布局中使用,裡面含有屬性) 
  40.      * @param context 調用自訂控制項的對象 
  41.      * @param attrs 
  42.      */  
  43.     public mycontrol(Context context, AttributeSet attrs) {  
  44.         super(context, attrs);  
  45.           
  46.         _Context = context;  
  47.         //將自訂的控制項添加到主布局  
  48.         this.addView(CreateLayout());  
  49.     }  
  50.   
  51.     private View CreateLayout(){  
  52.         //建立一個LainearLayout布局  
  53.         LinearLayout layout = new LinearLayout(_Context);  
  54.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);  
  55.         layout.setOrientation(LinearLayout.VERTICAL);  
  56.         layout.setLayoutParams(params);  
  57.         //建立一個文本編輯框  
  58.         final EditText edit = new EditText(_Context);  
  59.         LayoutParams editParams = new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);  
  60.         edit.setLayoutParams(editParams);  
  61.         //建立一個按鈕  
  62.         Button button = new Button(_Context);  
  63.         LayoutParams btpParams = new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT);  
  64.         button.setLayoutParams(btpParams);  
  65.         button.setText("點擊擷取");  
  66.         //設定按鈕的點擊事件  
  67.         button.setOnClickListener(new OnClickListener() {  
  68.               
  69.             @Override  
  70.             public void onClick(View v) {  
  71.                 // 返回這個自訂控制項中計算出的值,使用回調實現  
  72.                 icallBack.onClickButton(edit.getText().toString());  
  73.             }  
  74.         });  
  75.         //文本編輯框和按鈕添加到layout布局  
  76.         layout.addView(edit);  
  77.         layout.addView(button);  
  78.           
  79.         return layout;  
  80.     }  
  81.   
  82. }  



 

activity_main.xml代碼

 

[html] view plaincopy 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     <!-- 定義自己的控制項 -->  
  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.     <!-- 自訂控制項 -->  
  9.     <paj.control.mycontrol android:id="@+id/mycontrol"   
  10.         android:layout_width="fill_parent"   
  11.         android:layout_height="wrap_content"   
  12.         />  
  13.       
  14.     <!-- 顯示自訂控制項返回的值 -->  
  15.     <TextView android:id="@+id/test"   
  16.         android:layout_width="match_parent"   
  17.         android:layout_height="match_parent"  
  18.         />  
  19.    
  20. </LinearLayout>  

 

 


MainActivity.java 代碼:

 

[java] view plaincopy 
    1. package com.example.controlstest;  
    2.   
    3. import paj.control.mycontrol;  
    4. import paj.control.mycontrol.ICoallBack;  
    5. import android.os.Bundle;  
    6. import android.app.Activity;  
    7. import android.view.Menu;  
    8. import android.widget.TextView;  
    9.   
    10. public class MainActivity extends Activity {  
    11.   
    12.     mycontrol _mMycontrol;  
    13.     TextView textView;  
    14.       
    15.     @Override  
    16.     protected void onCreate(Bundle savedInstanceState) {  
    17.         super.onCreate(savedInstanceState);  
    18.         setContentView(R.layout.activity_main);  
    19.         textView = (TextView)findViewById(R.id.test);  
    20.         //得到自訂控制項  
    21.         _mMycontrol = (mycontrol)findViewById(R.id.mycontrol);  
    22.         //實現自訂控制項中的setonClick自訂事件  
    23.         _mMycontrol.setonClick(new ICoallBack() {  
    24.               
    25.             @Override  
    26.             public void onClickButton(String s) {  
    27.                 textView.setText(s);//將自訂控制項傳遞的值顯示到文字框內  
    28.             }  
    29.         });  
    30.     }  
    31. }

android 自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.