- 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() {
- 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);
- this.editText.setInputType(InputType.TYPE_CLASS_TEXT);
- if (password) {
- .setTransformationMethod(PasswordTransformationMethod
- .getInstance());
- this.editText.setTransformationMethod(null);
- public void show(View parent, String hint, String text) {
- if (hint != null) {
- this.editText.setHint(hint);
- this.editText.setHint("");
- if (text != null) {
- this.editText.setText(text);
- 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() {
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- public void afterTextChanged(Editable s) {
- if (onSoftInputListener != null) {
- onSoftInputListener.onTextChanged(s.toString());
- /**
- * 弹出软键盘
- *
- * @param activity
- * 不解释
- * @param hint
- * 如果没有,输入null
- * @param text
- * @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,
- 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);
- }
复制代码
使用时直接调用 SoftInput.showSoftInput() 方法弹出软件盘,在 回调 onSoftInputListener 监听输入结果。
http://www.eoeandroid.com/forum-863-1.html