自定義view--帶下劃線的EditText

一、需求:

由於項目中要用到帶下滑線的EditTextView(大體如下圖,這也是本demo的效果圖),本來說做一個背影圖片放置即可,最後一想還是練習一下自定義view吧,反正自己的自定義view很爛。索性就自定義一個。


二、動手之前:

動手去做之前,先簡單說一下思路,關於自定義view的步驟這裏不說明,以後會寫相關文章來說明的。這裏我們選擇繼承EditText來自定義我們的View。無非就是畫一條直線而已,畫的時候x從0到view.getWidth(),y人view.getHeight到view.getHeight()即可。當然線的顏色和線的高度我們可以設置自定義屬性來讓xml去動態加載。

三、直接動手:

1、先自定義我們要屬性,大概要一個下滑線的顏色和高度就可以了。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <!-- 定義自定義屬性 -->
    <attr name="line_color" format="color"/>
    <attr name="line_height" format="dimension"/>
    <!-- 聲明自定義屬性 -->
    <declare-styleable name="edittext_bg">
        <attr name="line_color"/>
        <attr name="line_height"/>
    </declare-styleable>
</resources>

2、獲取自定義屬性,一般在三個參數的構造方法中取,當然也可以在兩個參數的構造方法中去取。

public class CusTomLineEditText extends EditText {
	
	
	/**下滑線的顏色**/
	private int line_corlor  ;
	
	/**下滑線的高度**/
	private int line_height ;
	
	/**畫筆**/
	private Paint mPaint ;
	
	
	public CusTomLineEditText(Context context){
		//調用兩個參數的構造方法
		this(context,null) ;
	}
	/**
	 * 加載xml的時候調用這個構造方法
	 * @param context
	 * @param attrs
	 */
	public CusTomLineEditText(Context context, AttributeSet attrs){
		//調用三個參數的構造方法
		this(context,attrs,0) ;
	}

	public CusTomLineEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		initTypeArray(context, attrs, defStyle);
		initPaint();
	}
	/**初始化Paint***/
	private void initPaint() {
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(line_height) ;
		mPaint.setColor(line_corlor);
	}
	/***
	 * 初始化TypedArray
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	private void initTypeArray(Context context, AttributeSet attrs, int defStyle) {
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.edittext_bg, defStyle, 0) ;
		int n = a.getIndexCount() ;
		for (int i = 0; i < n; i++) {
			int attr = a.getIndex(i) ;
			switch (attr) {
			case R.styleable.edittext_bg_line_color:
				//從xml取得下滑線的顏色 默認是綠色
				line_corlor = a.getColor(attr, Color.GREEN) ;
				break;
			case R.styleable.edittext_bg_line_height:
				//從xml取得下滑線的高 默認是1dip   dip to px
				line_height = a.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics())) ;
				break ;
			}
		}
		//使用完一定要recycle 保證資源回收
		a.recycle() ;
	}

代碼都很簡單,註釋也詳細,不做細緻的解析,相信都能看懂。

3、我們這裏不需要重寫OnMeasure(),直接重寫Ondraw()即可

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 畫底線
		canvas.drawLine(0, this.getHeight(), this.getWidth(),this.getHeight(), mPaint);
	}

經過以上三步就可以製作屬於我們的帶下滑線的自定義EditText了。

4、使用自定義EditText.

首先在佈局文件中一般在根佈局中指定我們自定義view的全名空間,以致於我們能找到自定義的屬性。

<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:jun="http://schemas.android.com/apk/res/com.jun.customlineedittext"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip" >

        <TextView
            android:id="@+id/tv_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="User:" />

        <com.jun.customlineedittext.view.CusTomLineEditText
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/tv_user"
            android:background="@null"
            android:gravity="center_vertical"
            jun:line_color="#7ac943"
            jun:line_height="2dip" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip" >

        <TextView
            android:id="@+id/pass_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Pass:" />

        <com.jun.customlineedittext.view.CusTomLineEditText
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/pass_user"
            android:background="@null"
            android:gravity="center_vertical"
            android:hint="asdfasdf"
            jun:line_color="#7ac943"
            jun:line_height="2dip" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="15dip"
        android:gravity="center" >

        <Button
            android:id="@+id/cancel"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Cancel" />

        <Button
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dip"
            android:layout_toRightOf="@+id/cancel"
            android:text="Save" />
    </RelativeLayout>

</LinearLayout>

這樣就完成了我們開頭需求中給出的圖例demo了。

5、測試

直接運行手機跑起項目,發現EditText點擊不起作用,鍵盤彈不出來,what the fk?尼瑪,我是繼承EditText的雜會沒有EditText的原有屬性呢?難道要讓我拿代碼硬把鍵盤調出來?(本人親測,用代碼硬調,還是出不來鍵盤。)其實解決這個問題一只需要改動一行代碼即可,具體說一個常量即可。還記得我們的CustomLineEditText的兩個參數的構造方法嗎,是這樣寫的this(context,attrs,0)直接調用三個參數的構造方法。第三個參數是默認的樣式屬性,我們只需要把this(context,attrs,0)改成this(context,attrs,android.R.attr.editTextStyle)即可讓我們的EditText彈出鍵盤了。

6、以上就完成了一個自定義帶下線的EditText.如果有什麼不對,歡迎指正。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章