关于EditText的一些问题总结

2017-06-22  本文已影响261人  cccccccxxxxxx

    好久没来总结知识了,这个习惯不能丢。

今天写一个界面,要用到一个很基础的控件 -- EditText

然而出现了很多问题,下面一一列出问题和解决方法

1.监听EditText的文字输入

原答案链接

动态监听文字输入长度,可以给出提示,限制输入文字长度

监听器:

class EditChangedListener implements TextWatcher {

private CharSequence temp;//监听前的文本

private inteditStart;//光标开始位置

private inteditEnd;//光标结束位置

private finalintcharMaxNum =10;

@Override

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

if(DEBUG)

Log.i(TAG,"输入文本之前的状态");

temp = s;

}

@Override

public void onTextChanged(CharSequence s,int start,int before,int count) {

if(DEBUG)

Log.i(TAG,"输入文字中的状态,count是一次性输入字符数");

mTvAvailableCharNum.setText("还能输入"+ (charMaxNum - s.length()) +"字符");

}

@Override

public void afterTextChanged(Editable s) {

if(DEBUG)

Log.i(TAG,"输入文字后的状态");

/** 得到光标开始和结束位置 ,超过最大数后记录刚超出的数字索引进行控制 */

editStart = mEditTextMsg.getSelectionStart();

editEnd = mEditTextMsg.getSelectionEnd();

if(temp.length() > charMaxNum) {

Toast.makeText(getApplicationContext(),"你输入的字数已经超过了限制!", Toast.LENGTH_LONG).show();

s.delete(editStart -1, editEnd);

int tempSelection = editStart;

mEditTextMsg.setText(s);

mEditTextMsg.setSelection(tempSelection);

}

}

};

给EditText添加监听:

mEditTextMsg.addTextChangedListener(new EditChangedListener());

2.和输入法的冲突,点开输入法后被遮挡

第一种解决方法,在manifest文件中,给对应的Activity增加一行代码:

android:windowSoftInputMode="adjustResize"

第二种解决方法,写一个监听器,通过监听软键盘是否打开或关闭来进行相应操作,代码中加粗的注释部分就是思路,此方法有点麻烦,但也是一种思路,以后找到更好的方法再来更新

/**

* 软键盘的监听

*/

public class KeyBoardShowListener {

private Context ctx;

public KeyBoardShowListener(Context ctx) {

this.ctx = ctx;

}

OnKeyboardVisibilityListener keyboardListener;

public OnKeyboardVisibilityListener getKeyboardListener() {

return keyboardListener;

}

public interface OnKeyboardVisibilityListener {

void onVisibilityChanged(boolean visible);

}

public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {

final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

private boolean wasOpened;

private final int DefaultKeyboardDP = 100;

// From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff

private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);

private final Rect r = new Rect();

@Override

public void onGlobalLayout() {

// Convert the dp to pixels.

int estimatedKeyboardHeight = (int) TypedValue

.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());

// Conclude whether the keyboard is shown or not.

activityRootView.getWindowVisibleDisplayFrame(r);

int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);

boolean isShown = heightDiff >= estimatedKeyboardHeight;

if (isShown == wasOpened) {

Log.e("Keyboard state", "Ignoring global layout change...");

return;

}

wasOpened = isShown;

listener.onVisibilityChanged(isShown);

}

});

}

}

//监听软键盘的状态

new KeyBoardShowListener(Activity.this).setKeyboardListener(

new KeyBoardShowListener.OnKeyboardVisibilityListener() {

@Override

public void onVisibilityChanged(boolean visible) {

if (visible) {

//软键盘已弹出

//隐藏多余界面,只展示EditText

} else {

//软键盘未弹出

//显示全部界面

}

}

}, Activity.this);

3.打开界面后,EditText直接获取焦点,打开输入法

在布局界面中,给EditText的父布局增加两行代码

android:focusable="true"

android:focusableInTouchMode="true"

4.修改EditText中的文字光标位置

EditText et = ...findViewById(...);

String text = "text";

et.setText(text);

et.setSelection(text.length());

其他一些有关EditText的内容:

---> 绑定软键盘到EditText

edit.setFocusable(true);

edit.setFocusableInTouchMode(true);

edit.requestFocus();

InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

inputManager.showSoftInput(edit, 0);

---> 去除软键盘显示:

editMsgView.setText("");

editMsgView.clearFocus();

//close InputMethodManager

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

--->始终不弹出软件键盘:

EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

或者这样也可以:

InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

if(imm.isActive()){ //这里可以判断也可以不判断

imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );

}

设置EditText输入类型

例如:身份证

android:digits="0123456789xX"

android:inputType="number"

CoderTung的第8篇知识储备。

上一篇 下一篇

猜你喜欢

热点阅读