[android]editText和软键盘的一些总结

总结一下最近用到的知识点。参考了很多人的资料这里不一一列出了,十分感谢。

一、editText默认设置不选中

在xml中在EditText父节点增加 
android:focusable="true" 
android:focusableInTouchMode="true" 表示将焦点给EditText的父节点。

二、editText主动失去焦点

EditText editText;
editText.clearFocus()

三、editText的文字变化监听

        editText.addTextChangedListener(new TextWatcher() {

            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
            
                    
            }
        });

以上是最基本格式,实际情况可以重写TextWatcher。


四、点击空白处隐藏软键盘

 // 获取点击事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
                clearAllFocus();
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


亲测适用于各类activity和Fragment。


五、改变enter键的属性

private OnKeyListener onKeyListener = new OnKeyListener() {
		
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
				/*隐藏软键盘*/
				InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				if(inputMethodManager.isActive()){
					inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
				}
				
				
				return true;
			}
			return false;
		}
	};

我这里按回车键的反馈是隐藏键盘。根据实际情况修改或者改写,之后调用

edittext.setOnKeyListener(onKeyListener); 

精确的判断右下角按键情况,以便应对更加复杂的情况。

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {        
            @Override  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                /*判断是否是“GO”键*/  
                if(actionId == EditorInfo.IME_ACTION_GO){  
                    /*隐藏软键盘*/  
                    InputMethodManager imm = (InputMethodManager) v  
                            .getContext().getSystemService(  
                                    Context.INPUT_METHOD_SERVICE);  
                    if (imm.isActive()) {  
                        imm.hideSoftInputFromWindow(  
                                v.getApplicationWindowToken(), 0);  
                    }  
                    return true;  
                }  
                return false;  
            }  
        });  

actionNone : 回车键,按下后光标到下一行

actionGo : Go,
actionSearch : 放大镜
actionSend : Send
actionNext : Next
actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框

<EditText  
        android:id="@+id/edittext"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:singleLine="true"  
        android:imeOptions="actionSearch"/>

改变方法就是给EditText控件的imeOptions属性设置成不同的值(此时Enter键可以显示不同的文字和图案)。
以上,后续再碰到其他问题还会继续修改。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值