目录
1.去掉下面的横线
加个背景即可
设置选中时背景
http://www.jianshu.com/p/7257ce82c762
android:background="@null"
android:drawableBottom="@drawable/selector_edittext_line"
2.点击Activity任意地方关闭软键盘
http://www.it165.net/pro/html/201405/13742.html
3.设置光标位置
setSelection(addressBean.uname.length());
4.addTextChangedListener监听的死循环问题
https://blog.csdn.net/qq_21229739/article/details/50896029
在afterTextChanged方法里定义boolean标记变量
5.修改键盘里的回车
http://blog.csdn.net/wang_shaner/article/details/8467688
EditText中imeOptions属性使用及设置无效解决
http://blog.csdn.net/lastdream/article/details/24365633
经过试验 设置下面两个属性中的一个即可使这个属性生效(应该还有其他的属性也可以,没去试验)
1 将singleLine设置为true
2 将inputType设置为text
6.不触摸EditText就弹出软键盘
http://www.360doc.com/content/14/0722/10/11800748_396211794.shtml
etMainContent.requestFocus();
InputMethodManager imm = (InputMethodManager) etMainContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
7.默认让某个EditText获取焦点
searchView.setFocusable(true);
searchView.setFocusableInTouchMode(true);
searchView.requestFocus();
searchView.clearFocus();//失去焦点
searchView.requestFocus();//获取焦点
8.带删除的EditText
http://blog.csdn.net/xuyonghong1122/article/details/46663601#
http://blog.csdn.net/xiaanming/article/details/11066685/
9.限制EditText输入小数点后面位数
https://blog.csdn.net/b7223058/article/details/77839556
10.改变光标
1.在资源文件drawable下新建一个光标控制color_cursor.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#008000" />
</shape>
2.设置EditText:android:textCursorDrawable="@drawable/color_cursor"
11.将密码显示的点号改成*号
https://blog.csdn.net/weixin_33541245/article/details/117840731
EditText UPL =(EditText) findViewById(R.id.UserPasswordToLogin) ;
UPL.setTransformationMethod(new AsteriskPasswordTransformationMethod());
然后创建一个新的Java类,名为AsteriskPasswordTransformationMethod.java延伸
这里是代码:
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
@Override
public char charAt(int index) {
return '*'; // This is the important part
}
@Override
public int length() {
return mSource.length(); // Return default
}
@Override
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}
12.明文与密文显示
if (isChecked) {
//明文
mBinding.editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
mBinding.editText.setTransformationMethod(null);
} else {
//密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用
mBinding.editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//将密码输入框的点号变成*号
mBinding.editText.setTransformationMethod(asteriskPasswordTransformationMethod);
}
当设置明文与密文时,光标会自动跳到头,如何解决。
原因:首先上面的这段代码会引起EditText的onSelectionChanged方法回调(0,0)
自定义EditTextView,记录光标位置。
public class CursorWatchEditTextView extends androidx.appcompat.widget.AppCompatEditText {
/**
* 是否记录光标位置.
* 明密文切换时会触发{@link #onSelectionChanged(int, int)}
* 时,此时不应该记录.
*/
private boolean recordCursorIndex = true;
/**
* 光标位置 .
*/
private int cursorIndex = 0;
public CursorWatchEditTextView(Context context) {
super(context);
}
public CursorWatchEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CursorWatchEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
LogUtils.d("TAG", "onSelectionChanged selStart = " + selStart + ",selEnd = " + selEnd);
if (recordCursorIndex) {
this.cursorIndex = selStart;
} else {
LogUtils.d("TAG", "onSelectionChanged but recordCursorIndex is false");
}
}
public void setRecordCursorIndex(boolean recordCursorIndex) {
this.recordCursorIndex = recordCursorIndex;
}
/**
* 恢复光标位置.
*/
public void restoreCurPosition() {
setSelection(cursorIndex);
}
}
然后执行
//不记录光标变化
mBinding.editText.setRecordCursorIndex(false);
执行明密文设置方法(引起光标变化)
//记录光标变化
mBinding.editText.setRecordCursorIndex(true);
//恢复光标位置
mBinding.editText.restoreCurPosition();
//密码是否可见控制 .
mBinding.cbInputVisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mBinding.editText.setRecordCursorIndex(false);
if (isChecked) {
//明文
mBinding.editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
mBinding.editText.setTransformationMethod(null);
} else {
//密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用
mBinding.editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//将密码输入框的点号变成*号
mBinding.editText.setTransformationMethod(asteriskPasswordTransformationMethod);
}
mBinding.editText.setRecordCursorIndex(true);
mBinding.editText.restoreCurPosition();
}
});
0 条评论