89 lines
2.6 KiB
Plaintext
Executable File
89 lines
2.6 KiB
Plaintext
Executable File
package com.example.administrator.ximengjianyu.utils;
|
||
|
||
import android.app.Activity;
|
||
import android.text.Editable;
|
||
import android.text.TextWatcher;
|
||
import android.view.View;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageView;
|
||
|
||
/**
|
||
* Created by xukai on 2017/1/11.
|
||
* 该类是对于Edittext的一些操作吧
|
||
*/
|
||
public class EdittextUtils {
|
||
|
||
public String TAG = "EdittextUtils";
|
||
|
||
private static EdittextUtils mEditText;
|
||
|
||
private EdittextUtils() {
|
||
}
|
||
private static void syncInit(){
|
||
if (mEditText == null){
|
||
mEditText = new EdittextUtils();
|
||
}
|
||
}
|
||
public static EdittextUtils getEdittext(){
|
||
if (mEditText == null){
|
||
syncInit();
|
||
}
|
||
return mEditText;
|
||
}
|
||
|
||
/**
|
||
* 创建方法,设置EditText的光标指向最后一位
|
||
*/
|
||
public void makeEdittextToLast(EditText edittext){
|
||
edittext.setCursorVisible(true);
|
||
edittext.setSelection(edittext.getText().length());
|
||
}
|
||
|
||
/**
|
||
* created by xukai:
|
||
* 该方法用来清除editText的相关内容
|
||
*/
|
||
public void clearEdittext(final EditText et, final ImageView iv) {
|
||
String text = et.getText().toString();
|
||
if (text == null) {//用户名为空,删除不可见
|
||
iv.setVisibility(View.GONE);
|
||
} else if (text.length() == 0) { //不为空,长度=0,也不可见
|
||
iv.setVisibility(View.GONE);
|
||
} else if (text.length() > 0) { //不为空,长度大于0
|
||
iv.setVisibility(View.VISIBLE);
|
||
}
|
||
et.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) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void afterTextChanged(Editable s) {
|
||
if(et.getText().length()==0){
|
||
iv.setVisibility(View.GONE);
|
||
return;
|
||
}
|
||
iv.setVisibility(View.VISIBLE); //设置清除内容图标可见
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* EditText获取焦点并显示软键盘
|
||
*/
|
||
public void showSoftInputFromWindow(Activity activity, EditText editText) {
|
||
editText.setFocusable(true);
|
||
editText.setFocusableInTouchMode(true);
|
||
editText.requestFocus();
|
||
InputUtils utils = new InputUtils();
|
||
utils.showSoftInput(activity);
|
||
}
|
||
}
|