亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Android自定義控件

系統(tǒng) 1819 0

今天和大家分享下組合控件的使用。很多時候android自定義控件并不能滿足需求,如何做呢?很多方法,可以自己繪制一個,可以通過繼承基礎(chǔ)控件來重寫某些環(huán)節(jié),當然也可以將控件組合成一個新控件,這也是最方便的一個方法。今天就來介紹下如何使用組合控件,將通過兩個實例來介紹。

第一個實現(xiàn)一個帶圖片和文字的按鈕,如圖所示:

整個過程可以分四步走。第一步,定義一個layout,實現(xiàn)按鈕內(nèi)部的布局。代碼如下:

  1. <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
  2. < LinearLayout ? xmlns:android = "http://schemas.android.com/apk/res/android" ??
  3. ???? android:orientation = "horizontal" ??
  4. ???? android:layout_width = "fill_parent" ??
  5. ???? android:layout_height = "fill_parent" ??
  6. ???? > ??
  7. < ImageView ??
  8. ???? android:layout_width = "wrap_content" ??
  9. ???? android:layout_height = "wrap_content" ??
  10. ???? android:id = "@+id/iv" ??
  11. ???? android:src = "@drawable/confirm" ??
  12. ???? android:paddingTop = "5dip" ??
  13. ???? android:paddingBottom = "5dip" ??
  14. ???? android:paddingLeft = "40dip" ??
  15. ???? android:layout_gravity = "center_vertical" ??
  16. ???? /> ??
  17. < TextView ??
  18. ???? android:layout_width = "wrap_content" ??
  19. ???? android:layout_height = "wrap_content" ??
  20. ???? android:text = "確定" ??
  21. ???? android:textColor = "#000000" ??
  22. ???? android:id = "@+id/tv" ??
  23. ???? android:layout_marginLeft = "8dip" ??
  24. ???? android:layout_gravity = "center_vertical" ??
  25. ???? /> ??
  26. </ LinearLayout > ??
      <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv"
    android:src="@drawable/confirm"
    android:paddingTop="5dip"
    android:paddingBottom="5dip"
    android:paddingLeft="40dip"
    android:layout_gravity="center_vertical"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="確定"
    android:textColor="#000000"
    android:id="@+id/tv"
    android:layout_marginLeft="8dip"
    android:layout_gravity="center_vertical"
    />
</LinearLayout>
    

這個xml實現(xiàn)一個左圖右字的布局,接下來寫一個類繼承LinearLayout,導入剛剛的布局,并且設(shè)置需要的方法,從而使的能在代碼中控制這個自定義控件內(nèi)容的顯示。代碼如下:

  1. package ?com.notice.ib; ??
  2. ??
  3. import ?android.content.Context; ??
  4. import ?android.util.AttributeSet; ??
  5. import ?android.view.LayoutInflater; ??
  6. import ?android.widget.ImageView; ??
  7. import ?android.widget.LinearLayout; ??
  8. import ?android.widget.TextView; ??
  9. ??
  10. public ? class ?ImageBt? extends ?LinearLayout?{ ??
  11. ??
  12. ???? private ?ImageView?iv; ??
  13. ???? private ?TextView??tv; ??
  14. ??
  15. ???? public ?ImageBt(Context?context)?{ ??
  16. ???????? this (context,? null ); ??
  17. ????} ??
  18. ??
  19. ???? public ?ImageBt(Context?context,?AttributeSet?attrs)?{ ??
  20. ???????? super (context,?attrs); ??
  21. ???????? //?導入布局 ??
  22. ????????LayoutInflater.from(context).inflate(R.layout.custombt,? this ,? true ); ??
  23. ????????iv?=?(ImageView)?findViewById(R.id.iv); ??
  24. ????????tv?=?(TextView)?findViewById(R.id.tv); ??
  25. ??
  26. ????} ??
  27. ??
  28. ???? /** ?
  29. ?????*?設(shè)置圖片資源 ?
  30. ?????*/ ??
  31. ???? public ? void ?setImageResource( int ?resId)?{ ??
  32. ????????iv.setImageResource(resId); ??
  33. ????} ??
  34. ??
  35. ???? /** ?
  36. ?????*?設(shè)置顯示的文字 ?
  37. ?????*/ ??
  38. ???? public ? void ?setTextViewText(String?text)?{ ??
  39. ????????tv.setText(text); ??
  40. ????} ??
  41. ??
  42. }??
      package com.notice.ib;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImageBt extends LinearLayout {

    private ImageView iv;
    private TextView  tv;

    public ImageBt(Context context) {
        this(context, null);
    }

    public ImageBt(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 導入布局
        LayoutInflater.from(context).inflate(R.layout.custombt, this, true);
        iv = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);

    }

    /**
     * 設(shè)置圖片資源
     */
    public void setImageResource(int resId) {
        iv.setImageResource(resId);
    }

    /**
     * 設(shè)置顯示的文字
     */
    public void setTextViewText(String text) {
        tv.setText(text);
    }

}
    

第三步,在需要使用這個自定義控件的layout中加入這控件,只需要在xml中加入即可。方法如下:

  1. < RelativeLayout ??
  2. ????????? android:orientation = "horizontal" ??
  3. ????????? android:layout_width = "fill_parent" ??
  4. ????????? android:layout_height = "wrap_content" ??
  5. ????????? android:layout_gravity = "bottom" ??
  6. ????????? > ??
  7. ????????? < com.notice.ib.ImageBt ??
  8. ????????????? android:id = "@+id/bt_confirm" ??
  9. ????????????? android:layout_height = "wrap_content" ??
  10. ????????????? android:layout_width = "wrap_content" ??
  11. ????????????? android:layout_alignParentBottom = "true" ??
  12. ????????????? android:background = "@drawable/btbg" ??
  13. ????????????? android:clickable = "true" ??
  14. ????????????? android:focusable = "true" ??
  15. ????????????? /> ??
  16. ????????? < com.notice.ib.ImageBt ??
  17. ????????????? android:id = "@+id/bt_cancel" ??
  18. ????????????? android:layout_toRightOf = "@id/bt_confirm" ??
  19. ????????????? android:layout_height = "wrap_content" ??
  20. ????????????? android:layout_width = "wrap_content" ??
  21. ????????????? android:layout_alignParentBottom = "true" ??
  22. ????????????? android:background = "@drawable/btbg" ??
  23. ????????????? android:clickable = "true" ??
  24. ????????????? android:focusable = "true" ??
  25. ???????????? /> ??
  26. ????????? </ RelativeLayout > ??
      <RelativeLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         >
         <com.notice.ib.ImageBt
             android:id="@+id/bt_confirm"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:layout_alignParentBottom="true"
             android:background="@drawable/btbg"
             android:clickable="true"
             android:focusable="true"
             />
         <com.notice.ib.ImageBt
             android:id="@+id/bt_cancel"
             android:layout_toRightOf="@id/bt_confirm"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:layout_alignParentBottom="true"
             android:background="@drawable/btbg"
             android:clickable="true"
             android:focusable="true"
            />
         </RelativeLayout>
    

注意的是,控件標簽使用完整的類名即可。為了給按鈕一個點擊效果,你需要給他一個selector背景,這里就不說了。

最后一步,即在activity中設(shè)置該控件的內(nèi)容。當然,在xml中也可以設(shè)置,但是只能設(shè)置一個,當我們需要兩次使用這樣的控件,并且顯示內(nèi)容不同時就不行了。在activity中設(shè)置也非常簡單,我們在ImageBt這個類中已經(jīng)寫好了相應(yīng)的方法,簡單調(diào)用即可。代碼如下:

  1. public ? class ?MainActivity? extends ?Activity?{ ??
  2. ??
  3. ???? private ?ImageBt?ib1; ??
  4. ???? private ?ImageBt?ib2; ??
  5. ??
  6. ???? /**?Called?when?the?activity?is?first?created.?*/ ??
  7. ???? @Override ??
  8. ???? public ? void ?onCreate(Bundle?savedInstanceState)?{ ??
  9. ???????? super .onCreate(savedInstanceState); ??
  10. ????????setContentView(R.layout.login); ??
  11. ??
  12. ????????ib1?=?(ImageBt)?findViewById(R.id.bt_confirm); ??
  13. ????????ib2?=?(ImageBt)?findViewById(R.id.bt_cancel); ??
  14. ??
  15. ????????ib1.setTextViewText( "確定" ); ??
  16. ????????ib1.setImageResource(R.drawable.confirm); ??
  17. ????????ib2.setTextViewText( "取消" ); ??
  18. ????????ib2.setImageResource(R.drawable.cancel); ??
  19. ??
  20. ????????ib1.setOnClickListener( new ?OnClickListener()?{ ??
  21. ??
  22. ???????????? @Override ??
  23. ???????????? public ? void ?onClick(View?v)?{ ??
  24. ???????????????????? //在這里可以實現(xiàn)點擊事件 ??
  25. ????????????} ??
  26. ????????}); ??
  27. ??
  28. ????} ??
  29. }??
      public class MainActivity extends Activity {

    private ImageBt ib1;
    private ImageBt ib2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        ib1 = (ImageBt) findViewById(R.id.bt_confirm);
        ib2 = (ImageBt) findViewById(R.id.bt_cancel);

        ib1.setTextViewText("確定");
        ib1.setImageResource(R.drawable.confirm);
        ib2.setTextViewText("取消");
        ib2.setImageResource(R.drawable.cancel);

        ib1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    //在這里可以實現(xiàn)點擊事件
            }
        });

    }
}
    

這樣,一個帶文字和圖片的組合按鈕控件就完成了。這樣梳理一下,使用還是非常簡單的。組合控件能做的事還非常多,主要是在類似上例中的ImageBt類中寫好要使用的方法即可。

再來看一個組合控件,帶刪除按鈕的EidtText。即在用戶輸入后,會出現(xiàn)刪除按鈕,點擊即可取消用戶輸入。

定義方法和上例一樣。首先寫一個自定義控件的布局:

  1. <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
  2. < RelativeLayout ? xmlns:android = "http://schemas.android.com/apk/res/android" ??
  3. ???? android:layout_width = "fill_parent" ??
  4. ???? android:layout_height = "fill_parent" ??
  5. ???? > ??
  6. < EditText ??
  7. ???? android:id = "@+id/et" ??
  8. ???? android:layout_width = "fill_parent" ??
  9. ???? android:layout_height = "wrap_content" ??
  10. ???? android:singleLine = "true" ??
  11. ???? /> ??
  12. < ImageButton ??
  13. ???? android:id = "@+id/ib" ??
  14. ???? android:visibility = "gone" ??
  15. ???? android:src = "@drawable/menu_delete" ??
  16. ???? android:layout_width = "wrap_content" ??
  17. ???? android:layout_height = "wrap_content" ??
  18. ???? android:background = "#00000000" ??
  19. ???? android:layout_alignRight = "@+id/et" ? /> ??
  20. </ RelativeLayout > ??
      <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
    android:id="@+id/et"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    />
<ImageButton
    android:id="@+id/ib"
    android:visibility="gone"
    android:src="@drawable/menu_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:layout_alignRight="@+id/et" />
</RelativeLayout>
    

實現(xiàn)輸入框右側(cè)帶按鈕效果,注意將按鈕隱藏。然后寫一個EditCancel類,實現(xiàn)刪除用戶輸入功能。這里用到了TextWatch這個接口,監(jiān)聽輸入框中的文字變化。使用也很簡單,實現(xiàn)他的三個方法即可。看代碼:

  1. package ?com.notice.ce; ??
  2. ??
  3. import ?android.content.Context; ??
  4. import ?android.text.Editable; ??
  5. import ?android.text.TextWatcher; ??
  6. import ?android.util.AttributeSet; ??
  7. import ?android.view.LayoutInflater; ??
  8. import ?android.view.View; ??
  9. import ?android.widget.EditText; ??
  10. import ?android.widget.ImageButton; ??
  11. import ?android.widget.LinearLayout; ??
  12. ??
  13. public ? class ?EditCancel? extends ?LinearLayout? implements ?EdtInterface?{ ??
  14. ??
  15. ????ImageButton?ib; ??
  16. ????EditText????et; ??
  17. ??
  18. ???? public ?EditCancel(Context?context)?{ ??
  19. ???????? super (context); ??
  20. ??
  21. ????} ??
  22. ??
  23. ???? public ?EditCancel(Context?context,?AttributeSet?attrs)?{ ??
  24. ???????? super (context,?attrs); ??
  25. ????????LayoutInflater.from(context).inflate(R.layout.custom_editview,? this ,? true ); ??
  26. ????????init(); ??
  27. ??
  28. ????} ??
  29. ??
  30. ???? private ? void ?init()?{ ??
  31. ????????ib?=?(ImageButton)?findViewById(R.id.ib); ??
  32. ????????et?=?(EditText)?findViewById(R.id.et); ??
  33. ????????et.addTextChangedListener(tw); //?為輸入框綁定一個監(jiān)聽文字變化的監(jiān)聽器 ??
  34. ???????? //?添加按鈕點擊事件 ??
  35. ????????ib.setOnClickListener( new ?OnClickListener()?{ ??
  36. ??
  37. ???????????? @Override ??
  38. ???????????? public ? void ?onClick(View?v)?{ ??
  39. ????????????????hideBtn(); //?隱藏按鈕 ??
  40. ????????????????et.setText( "" ); //?設(shè)置輸入框內(nèi)容為空 ??
  41. ????????????} ??
  42. ????????}); ??
  43. ??
  44. ????} ??
  45. ??
  46. ???? //?當輸入框狀態(tài)改變時,會調(diào)用相應(yīng)的方法 ??
  47. ????TextWatcher?tw?=? new ?TextWatcher()?{ ??
  48. ??
  49. ??????????????????????? @Override ??
  50. ??????????????????????? public ? void ?onTextChanged(CharSequence?s,? int ?start,? int ?before,? int ?count)?{ ??
  51. ??????????????????????????? //?TODO?Auto-generated?method?stub ??
  52. ??
  53. ???????????????????????} ??
  54. ??
  55. ??????????????????????? @Override ??
  56. ??????????????????????? public ? void ?beforeTextChanged(CharSequence?s,? int ?start,? int ?count,? int ?after)?{ ??
  57. ??????????????????????????? //?TODO?Auto-generated?method?stub ??
  58. ??
  59. ???????????????????????} ??
  60. ??
  61. ??????????????????????? //?在文字改變后調(diào)用 ??
  62. ??????????????????????? @Override ??
  63. ??????????????????????? public ? void ?afterTextChanged(Editable?s)?{ ??
  64. ??????????????????????????? if ?(s.length()?==? 0 )?{ ??
  65. ???????????????????????????????hideBtn(); //?隱藏按鈕 ??
  66. ???????????????????????????}? else ?{ ??
  67. ???????????????????????????????showBtn(); //?顯示按鈕 ??
  68. ???????????????????????????} ??
  69. ??
  70. ???????????????????????} ??
  71. ??
  72. ???????????????????}; ??
  73. ??
  74. ???? @Override ??
  75. ???? public ? void ?hideBtn()?{ ??
  76. ???????? //?設(shè)置按鈕不可見 ??
  77. ???????? if ?(ib.isShown())?ib.setVisibility(View.GONE); ??
  78. ??
  79. ????} ??
  80. ??
  81. ???? @Override ??
  82. ???? public ? void ?showBtn()?{ ??
  83. ???????? //?設(shè)置按鈕可見 ??
  84. ???????? if ?(!ib.isShown())?ib.setVisibility(View.VISIBLE); ??
  85. ??
  86. ????} ??
  87. ??
  88. } ??
  89. ??
  90. interface ?EdtInterface?{ ??
  91. ??
  92. ???? public ? void ?hideBtn(); ??
  93. ??
  94. ???? public ? void ?showBtn(); ??
  95. ??
  96. }??
      package com.notice.ce;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class EditCancel extends LinearLayout implements EdtInterface {

    ImageButton ib;
    EditText    et;

    public EditCancel(Context context) {
        super(context);

    }

    public EditCancel(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
        init();

    }

    private void init() {
        ib = (ImageButton) findViewById(R.id.ib);
        et = (EditText) findViewById(R.id.et);
        et.addTextChangedListener(tw);// 為輸入框綁定一個監(jiān)聽文字變化的監(jiān)聽器
        // 添加按鈕點擊事件
        ib.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideBtn();// 隱藏按鈕
                et.setText("");// 設(shè)置輸入框內(nèi)容為空
            }
        });

    }

    // 當輸入框狀態(tài)改變時,會調(diào)用相應(yīng)的方法
    TextWatcher tw = new TextWatcher() {

                       @Override
                       public void onTextChanged(CharSequence s, int start, int before, int count) {
                           // TODO Auto-generated method stub

                       }

                       @Override
                       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                           // TODO Auto-generated method stub

                       }

                       // 在文字改變后調(diào)用
                       @Override
                       public void afterTextChanged(Editable s) {
                           if (s.length() == 0) {
                               hideBtn();// 隱藏按鈕
                           } else {
                               showBtn();// 顯示按鈕
                           }

                       }

                   };

    @Override
    public void hideBtn() {
        // 設(shè)置按鈕不可見
        if (ib.isShown()) ib.setVisibility(View.GONE);

    }

    @Override
    public void showBtn() {
        // 設(shè)置按鈕可見
        if (!ib.isShown()) ib.setVisibility(View.VISIBLE);

    }

}

interface EdtInterface {

    public void hideBtn();

    public void showBtn();

}
    

在TextWatch接口的afterTextChanged方法中對文字進行判斷,若長度為0,就隱藏按鈕,否則,顯示按鈕。

另外,實現(xiàn)ImageButton(即那個叉)的點擊事件,刪除輸入框中的內(nèi)容,并隱藏按鈕。

后面兩步的實現(xiàn)就是加入到實際布局中,就不再寫出來了,和上例的步驟一樣的。最后顯示效果如圖:

?

?

學會靈活的使用組合控件,對UI開發(fā)會有很大幫助。有什么問題可以留言交流~

Android自定義控件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲自拍成人 | www.亚洲视频 | 免费亚洲网站 | 国产剧情一区二区 | 日日日夜夜操 | 亚洲整片| 国产欧美一区二区三区免费 | 色插视频| 99re热久久精品这里都是精品 | 欧美日产| 在线免费小视频 | 国产一久久香蕉国产线看观看 | 亚洲乱视频| 国产人成激情视频在线观看 | 深夜视频在线免费观看 | 日本免费网址 | 中文精品视频一区二区在线观看 | 精品亚洲一区二区在线播放 | 国产99在线 | 国产欧美精品午夜在线播放 | 免费特黄一级欧美大片 | 欧美一级高清毛片aaa | 日本一本二本免费播放视频 | 欧美成人一级视频 | 色中文字幕 | 99精品一区二区三区 | 9999毛片免费看| 久久精品视频观看 | 久久99热久久精品 | 国产色产综合色产在线观看视频 | 青青青国产精品国产精品久久久久 | 精品免费tv久久久久久久 | 成人午夜精品久久久久久久小说 | 国产一级一片免费播放 | 香蕉在线精品一区二区 | 久草狼人 | 亚洲国产激情一区二区三区 | 天天躁日日躁狠狠躁综合 | 骚婷婷 | 天天综合天天看夜夜添狠狠玩 | 国产欧美日韩精品一区二区三区 |