package com.classic.core.utils;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

/**
 * 文本输入框工具类
 *
 * @author Jack Tony
 * @date 2015/7/31
 */
public final class EditTextUtil {
    private EditTextUtil() { }

    /**
     * 限制内容长度
     */
    public static void lengthFilter(final EditText editText, final int length) {
        InputFilter[] filters = new InputFilter[1];

        filters[0] = new InputFilter.LengthFilter(length) {
            public CharSequence filter(@NonNull CharSequence source, int start, int end,
                @NonNull Spanned dest, int dstart, int dend) {
                if (dest.toString().length() >= length) {
                    return "";
                }
                return source;
            }
        };
        // Sets the list of input filters that will be used if the buffer is Editable. Has no effect otherwise.
        editText.setFilters(filters);
    }

    /**
     * 限制中文字符的长度
     */
    public static void lengthChineseFilter(final EditText editText, final int length) {
        InputFilter[] filters = new InputFilter[1];

        filters[0] = new InputFilter.LengthFilter(length) {
            public CharSequence filter(@NonNull CharSequence source, int start, int end,
                @NonNull Spanned dest, int dstart, int dend) {
                // 可以检查中文字符
                boolean isChinese = StringUtil.checkNameChese(source.toString());
                if (!isChinese || dest.toString().length() >= length) {
                    return "";
                }
                return source;
            }
        };
        // Sets the list of input filters that will be used if the buffer is Editable. Has no effect otherwise.
        editText.setFilters(filters);
    }

    /**
     * 设置密码是否可见
     */
    public static void passwordVisibleToggle(EditText editText) {
        if (editText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
            editText.setInputType(
                InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        } else {
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }


    /**
     * 设置文本，并将光标移动到文本末尾
     * @param editText
     * @param text
     */
    public static void setText(EditText editText,String text){
        if(null != editText && !TextUtils.isEmpty(text)){
            editText.setText(text);
            editText.setSelection(text.length());
        }
    }


    /**
     * 监听文本框内容，动态改变指定view状态
     * 文本框无内容则隐藏指定view,有内容则显示指定view
     * @param editText
     * @param view
     */
    public static void autoHideViewByTextChanged(EditText editText,final View view){
        if(null != editText && null != view){
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) { }


                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    view.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
                }


                @Override public void afterTextChanged(Editable s) { }
            });
        }
    }

    /**
     * 屏蔽复制、粘贴功能
     * 需要sdk>=11
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static void pasteUnable(EditText editText) {
        editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                return false;
            }

            @Override public void onDestroyActionMode(ActionMode mode) {

            }
        });
        editText.setLongClickable(false);
    }

}

@@ -2843,33 +2843,32 @@
        %7D%0A    %7D%0A%0A
-%0A
     /**%0A     * %E7%9B%91
@@ -2886,16 +2886,17 @@
 %E6%8C%87%E5%AE%9Aview%E7%8A%B6%E6%80%81
+%EF%BC%8C
 %0A     * 
@@ -2919,24 +2919,49 @@
 %E6%9C%89%E5%86%85%E5%AE%B9%E5%88%99%E6%98%BE%E7%A4%BA%E6%8C%87%E5%AE%9Aview
+;%0A     * %E7%82%B9%E5%87%BBview%E8%87%AA%E5%8A%A8%E6%B8%85%E7%90%86%E6%96%87%E6%9C%AC%E6%A1%86%E5%86%85%E5%AE%B9%E3%80%82
 %0A     * @par
@@ -2986,17 +2986,22 @@
  @param 
-v
+clearV
 iew%0A    
@@ -3035,30 +3035,20 @@
 auto
-HideViewByTextChanged(
+Clear(final 
 Edit
@@ -3072,17 +3072,22 @@
 al View 
-v
+clearV
 iew)%7B%0A  
@@ -3123,17 +3123,22 @@
 null != 
-v
+clearV
 iew)%7B%0A  
@@ -3321,25 +3321,24 @@
 after) %7B %7D%0A%0A
-%0A
             
@@ -3465,17 +3465,22 @@
         
-v
+clearV
 iew.setV
@@ -3554,25 +3554,24 @@
          %7D%0A%0A
-%0A
             
@@ -3637,32 +3637,234 @@
             %7D);%0A
+            clearView.setOnClickListener(new View.OnClickListener() %7B%0A                @Override public void onClick(View v) %7B%0A                    editText.setText(%22%22);%0A                %7D%0A            %7D);%0A
         %7D%0A    %7D%0A

add methods