import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.orange.ui.activity.GameActivity;
public class SoftInput {
private GameActivity activity;
private static OnSoftInputListener onSoftInputListener;
private EditText editText;
public SoftInput(GameActivity activity,
OnSoftInputListener onSoftInputListener) {
this.activity = activity;
SoftInput.onSoftInputListener = onSoftInputListener;
this.editText = activity.getEditText();
this.editText.setOnKeyListener(onKeyListener1);
}
private OnKeyListener onKeyListener1 = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
editText.setOnKeyListener(onKeyListener2);
return true;
}
return false;
}
};
private OnKeyListener onKeyListener2 = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
};
public void setEditViewStyle(boolean number, boolean password,
int maxTextLength) {
if (this.editText != null) {
if (maxTextLength > 0) {
this.editText
.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
maxTextLength) });
} else {
this.editText.setFilters(new InputFilter[] {});
}
if (number) {
this.editText.setInputType(InputType.TYPE_CLASS_NUMBER);
} else {
this.editText.setInputType(InputType.TYPE_CLASS_TEXT);
}
if (password) {
this.editText
.setTransformationMethod(PasswordTransformationMethod
.getInstance());
} else {
this.editText.setTransformationMethod(null);
}
}
}
public void show(View parent, String hint, String text) {
if (this.editText != null) {
if (hint != null) {
this.editText.setHint(hint);
} else {
this.editText.setHint("");
}
if (text != null) {
this.editText.setText(text);
} else {
this.editText.setText("");
}
Editable editable = this.editText.getText();
if (editable != null) {
this.editText.setSelection(editable.toString().length());
}
this.editText.removeTextChangedListener(mTextWatcher);
this.editText.addTextChangedListener(mTextWatcher);
final InputMethodManager m = (InputMethodManager) this.activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
m.showSoftInput(this.editText, 0);
}
}
private static TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (onSoftInputListener != null) {
onSoftInputListener.onTextChanged(s.toString());
}
}
};
/**
* 弹出软键盘
*
* @param activity
* 不解释
* @param hint
* 如果没有,输入null
* @param text
* 如果没有,输入null
* @param number
* 是否数字
* @param password
* 是否密码
* @param maxTextLength
* 最大长度,如果不限制,输入0
* @param onSoftInputListener
* 返回接口
*/
public static void showSoftInput(GameActivity activity, String hint,
String text, boolean number, boolean password, int maxTextLength,
OnSoftInputListener onSoftInputListener) {
SoftInput softInput = new SoftInput(activity, onSoftInputListener);
softInput.setEditViewStyle(number, password, maxTextLength);
softInput.show(activity.getWindow().getDecorView(), hint, text);
}
public interface OnSoftInputListener {
/**
* 编辑框内容改变
*
* @param newContent
* 新内容
*/
public void onTextChanged(String newContent);
}
}
复制代码
http://www.eoeandroid.com/forum-863-1.html
OGEngine 弹出软件盘手动输入文字处理,布布扣,bubuko.com
原文地址:http://9165326.blog.51cto.com/9155326/1439352