Android基础控件EditText

1、常用属性

        <!--selectAllOnFocus  获得焦点后全选组件内所有文本内容-->
        <!--inputType 限制输入方式-->
        <!--singleLine 是否单行显示-->
        <!--maxLines 最大行数超过文本自动上滚-->
        <!--scaleX scaleY 文字间隔-->
        <!--layout_marginLeft 控件自身左边偏移-->
        <!--paddingLeft 控件内部内容左边偏移-->
        <!--hint 提示语-->
        <!--textColorHint 提示语颜色-->
        <!--enabled 输入框是否可编辑-->
        <!--imeOptions 修改'完成'样式-->
        <!--textCursorDrawable 设置光标 null隐藏光标和字体颜色一致-->
        <!--background 背景图片-->
        <!--drawableBottom 通过shape自定义下划线-->

 

2、简单使用

layout布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LoginActivity">

        <!--selectAllOnFocus  获得焦点后全选组件内所有文本内容-->
        <!--inputType 限制输入方式-->
        <!--singleLine 是否单行显示-->
        <!--maxLines 最大行数超过文本自动上滚-->
        <!--scaleX scaleY 文字间隔-->
        <!--layout_marginLeft 控件自身左边偏移-->
        <!--paddingLeft 控件内部内容左边偏移-->
        <!--hint 提示语-->
        <!--textColorHint 提示语颜色-->
        <!--enabled 输入框是否可编辑-->
        <!--imeOptions 修改'完成'样式-->
        <!--textCursorDrawable 设置光标 null隐藏光标和字体颜色一致-->
        <!--background 背景图片-->
        <!--drawableBottom 通过shape自定义下划线-->
        <com.example.xianfengzhang.myapplication.EditTextDelete
            android:id="@+id/edit11"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:inputType="none"
            android:selectAllOnFocus="true"
            android:singleLine="false"
            android:minLines="1"
            android:maxLines="3"
            android:scaleX="1"
            android:scaleY="1"
            android:layout_marginLeft="5dp"
            android:paddingLeft="5dp"

        />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit22"
            android:hint="这是提示语……"
            android:textColorHint="@color/colorPrimaryDark"
            android:enabled="true"
            android:imeOptions="actionSearch"
            android:textCursorDrawable="@drawable/edit_cur_values"
            android:background="@null"
            android:drawableBottom="@drawable/edit_down_values"
            />


</LinearLayout>

自定义的 EditTextDelete

package com.example.xianfengzhang.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;

import java.util.jar.Attributes;

/**
 * Created by xianfengzhang on 2017/12/14.
 */

public class EditTextDelete extends android.support.v7.widget.AppCompatEditText {

    private  final static String TAG = "EditTextDelete";
    private Drawable imgInable;
    private Drawable imgAble;
    private Context mContest;

    public EditTextDelete(Context context){
        super(context);
        mContest = context;
        init();
    }
    public EditTextDelete(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContest = context;
        init();
    }

    public EditTextDelete(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContest = context;
        init();
    }


    private void init(){

        imgInable = mContest.getDrawable(R.drawable.del);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                setDrawable();
            }
        });
        setDrawable();
    }

    private  void  setDrawable(){
        if (length()<1)
            setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
        else
            setCompoundDrawablesWithIntrinsicBounds(null,null,imgInable,null);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgInable != null && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
            Rect rect = new Rect();
            getGlobalVisibleRect(rect);
            rect.left = rect.right - 100;
            if (rect.contains(eventX, eventY))
                setText("");
        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void finalize() throws Throwable{
        super.finalize();
    }

}

效果图

 

posted @ 2017-12-20 10:39  ForeverGuard  阅读(305)  评论(0编辑  收藏  举报