您当前的位置: 首页 >  ui

Kevin-Dev

暂无认证

  • 4浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- UI 开发】EditText 的基本使用

Kevin-Dev 发布时间:2016-12-02 14:27:09 ,浏览量:4

基本属性

EditText 官方文档

  • android:digits:指定特定能被输入的字符。

  • android:inputType:设定输入的类型,下面仅介绍一些常用的,多项可以使用“|”分割。 textUri:必须是一个URL。 textEmailAddress:Email地址 textPassword:密码。 number:数字。

  • android:numeric:指定数字输入类型,多项可以使用“|”分割。 integer:数字。 decimal:浮点类型。 signed:带符号。

  • android:imeOptions=“actionSearch” & singLine=“true” actionNext下一步,通常用于跳转到下一个EditText actionGo前往,通常用于打开链接 actionSend发送,通常用于发送信息 actionSearch搜索,通常用于搜索信息 actionDone确认,通常表示事情做完了

键盘挡住 EditText 的问题,解决方案: 在 EditText 所在的父布局设置 fitsSystemWindow=”true” 即可。

实例 一. 自带清除功能的 EditText

1. 效果图 image

2. 代码

/**
 * Created on 2019/6/5 10:12 AM
 *
 * @author GYQ
 */
@SuppressLint("AppCompatCustomView")
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
    private Drawable mClearDrawable;
    private boolean hasFocus;

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

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
            mClearDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_clear,null);
        }
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
        // 默认隐藏图标
        setDrawableVisible(false);
    }

    /**
     * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                int start = getWidth() - getTotalPaddingRight() + getPaddingRight();
                int end = getWidth();
                boolean available = (event.getX() > start) && (event.getX()  0) {
            setDrawableVisible(true);
        } else {
            setDrawableVisible(false);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        if (hasFocus) {
            setDrawableVisible(s.length() > 0);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    protected void setDrawableVisible(boolean visible) {
        Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }

}

3. 简单使用



二、自带下拉功能的EditText

1. 效果图 image

2. 代码

/**
 * Created on 2019/6/5 10:31 AM
 *
 * @author Scarf Gong
 */
@SuppressLint("AppCompatCustomView")
public class DropEditText extends EditText implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener {
    private Drawable mDrawable;
    private PopupWindow mPopupWindow;
    private ListView mPopListView;
    private int mDropDrawableResId;
    private int mRiseDrawableResID;

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

    public DropEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DropEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mPopListView = new ListView(context);
        mDropDrawableResId = R.drawable.ic_drop_down;
        mRiseDrawableResID = R.drawable.ic_drop_up;
        showDropDrawable(); // 默认显示下拉图标
        mPopListView.setOnItemClickListener(this);
    }

    /**
     * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟点击事件
     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                int start = getWidth() - getTotalPaddingRight() + getPaddingRight();
                int end = getWidth();
                boolean available = (event.getX() > start) && (event.getX() 

注意: 这里一定还要设置 singLine=“true”,不然回车还是换行的功能。

2. 常见的属性

  • actionNext下一步,通常用于跳转到下一个EditText

  • actionGo前往,通常用于打开链接

  • actionSend发送,通常用于发送信息

  • actionSearch搜索,通常用于搜索信息

  • actionDone确认,通常表示事情做完了

四、EditText 输入自带空格的手机号码

1. 效果图 这里写图片描述 2. 布局文件




    

    

3. 代码

public class MainActivity extends AppCompatActivity {
    private EditText mPhoneNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();

        initData();

    }
	private void initView() {
        mPhoneNum = (EditText)findViewById(R.id.editText);
    }
    private void initData() {
        mPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence == null || charSequence.length() == 0) {
                    return;
                }
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0;i= 0x25b6 && first = 0x23e9 && first = 0x1F000 && first = 0x2702) && (first = 0x1F601) && (first = 1) {//表情符号的字符长度最小为2
                        if (count - before == 20) {
                            input = s.toString().subSequence(start, count);
                        } else {
                            input = s.toString().subSequence(start, start + count);
                        }
                        if (EmojiTools.containsEmoji(input.toString())) {
                            resetText = true;
                            //是表情符号就将文本还原为输入表情符号之前的内容
                            ToastUtil.showToast(mContext.getString(R.string.base_xx_no_support));
                            setText(inputAfterText);
                            CharSequence text = getText();
                            if (text instanceof Spannable) {
                                Spannable spanText = (Spannable) text;
                                Selection.setSelection(spanText, text.length());
                            }
                        }
                    }
                } else {
                    resetText = false;
                }


            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}

8、InputView.java

public class InputView extends LinearLayout{
    private Context context;

    private ImageView imgIcon, imgDelete, imgPwd;
    private EditText edtText;
    private View bottomLine;
    private RelativeLayout deleteRl, pwdRl;

    private int iconFocusId;
    private int iconNormalId;
    private int iconDeleteId;
    private int iconShowPedId;
    private int iconHidePwdId;
    private String hintText = "";
    private String inputType = "";
    private boolean hasBottomLine = true;
    private boolean hasIcon = true;
    private boolean isPwdShow = false;

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

    public InputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
        initAttrs(attrs);
        initData();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.input_view);
        int n = a.getIndexCount();
        for (int i = 0; i             
关注
打赏
1658837700
查看更多评论
0.1243s