实例介绍
【实例截图】
【核心代码】
/**
*
*/
package com.example.edittextdemo;
import android.content.Context;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
/**
* @author jiashuai.xujs@alibaba-inc.com 2014-2-25 下午5:06:14
*
*/
public class FrontPartEditableText extends LinearLayout {
private Button button;
private int frontPartLength;
private BackDetectableEditText edittext;
private BuildTextContentListener listener;
public FrontPartEditableText(Context context) {
super(context);
init();
}
public FrontPartEditableText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FrontPartEditableText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());
View v = mLayoutInflater.inflate(R.layout.front_part_editable_text, null,false);
addView(v);
edittext = (BackDetectableEditText) v.findViewById(R.id.part_editable_text);
// 限定只能输入数字
edittext.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
// 可以获取焦点
button = (Button) v.findViewById(R.id.part_editable_text_dummy);
button.setFocusable(true);
button.setFocusableInTouchMode(true);
// http://stackoverflow.com/questions/3425932/detecting-when-user-has-dismissed-the-soft-keyboard
// 默认情况下,按返回会隐藏软键盘,拦截这个事件手动隐藏
edittext.setOnEditTextImeBackListener(new BackDetectableEditText.EditTextImeBackListener() {
@Override
public void onImeBack(BackDetectableEditText ctrl, String text) {
hideSoftInput();
}
});
// 一旦获取焦点,设置光标位置
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String mobile = getFrontPart(edittext.getText().toString());
setCursorPosition(mobile.length());
}
}
});
// 返回true,手动处理touch事件,即使edittext获取了焦点,也不会自动弹出软键盘,要手动弹出
// http://stackoverflow.com/questions/10263384/android-how-to-get-text-position-from-touch-event
edittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Layout layout = ((EditText) v).getLayout();
float x = event.getX() edittext.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if (offset >= 0 && offset < frontPartLength) {
edittext.setSelection(offset);
} else if (offset >= frontPartLength) {
edittext.setSelection(frontPartLength);
}
showSoftInput();
}
return true;
}
});
edittext.addTextChangedListener(new TextWatcher() {
private String preText;
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(listener == null){
throw new RuntimeException("BuildTextContentListener can not be empty");
}
String nowtext = listener.buildTextContent(s.toString());
if (nowtext.equals(preText)) {
return;
}
String frontPart = getFrontPart(nowtext);
// 计算当前的光标位置
int offset = calCursorOffset(preText, nowtext);
// 一定要在setTest之前设置preText,否则会StackOverflow
preText = nowtext;
edittext.setText(nowtext);
// 文字发生变化,重新设置光标,否则会跑到最前面
setCursorPosition(offset);
if (frontPart.length() == frontPartLength) {
hideSoftInput();
}
}
});
}
public void setBuildTextContentListener(BuildTextContentListener listener){
this.listener = listener;
}
public interface BuildTextContentListener{
public String buildTextContent(String text);
}
public void setFrontPartLength(int frontPartLength) {
this.frontPartLength = frontPartLength;
}
public void hideSoftInput() {
edittext.requestFocus();
Util.hideKeyboard(edittext);
button.requestFocus();
}
public void showSoftInput() {
edittext.requestFocus();
Util.showKeyboard(edittext);
}
private String getFrontPart(String text) {
if (text == null || text.length() <= 0) {
return "";
}
String arr[] = text.split("\\s");
String mobile = arr[0];
return mobile;
}
private void setCursorPosition(int offset) {
edittext.setSelection(offset);
}
private int calCursorOffset(String pre, String now) {
if (Util.isBlank(pre) && Util.isBlank(now)) {
return 0;
} else if (!Util.isBlank(pre) && !Util.isBlank(now)) {
for (int i = 0; i < pre.length() && i < now.length(); i ) {
int prechar = pre.charAt(i);
int nowchar = now.charAt(i);
if (prechar != nowchar) {
return i;
}
}
}
return now.length() > frontPartLength ? frontPartLength : now.length();
}
}
标签: Android
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论