pjsip视频通信开发(上层应用)之EditText重写
生活随笔
收集整理的這篇文章主要介紹了
pjsip视频通信开发(上层应用)之EditText重写
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們經(jīng)常使用手機(jī)的打電話功能,當(dāng)我們按鍵盤的時(shí)候,有一個(gè)地方顯示我們按鍵的內(nèi)容,當(dāng)我們的手點(diǎn)擊那個(gè)地方的時(shí)候,并沒有彈出軟件盤,所以我們?cè)儆袛?shù)字鍵盤的時(shí)候,要屏蔽系統(tǒng)的軟件盤。
我們分析一下,軟件盤彈出的條件:
1、焦點(diǎn),當(dāng)EditText處于焦點(diǎn)的時(shí)候,會(huì)自動(dòng)彈出軟件盤,所以我們要重寫onFocusChanged函數(shù)
2、觸摸時(shí)間,當(dāng)你點(diǎn)擊EditText的時(shí)候,那它就會(huì)處于焦點(diǎn),所以我們要重寫onTouchEvent函數(shù)
3、當(dāng)布局改變的時(shí)候,EditText也會(huì)處于焦點(diǎn),所以我們也應(yīng)該重寫一下layout函數(shù)
?
package com.jwzhangjie.pjsip.ui.dialpad;import android.content.Context; import android.graphics.Rect; import android.text.InputType; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.accessibility.AccessibilityEvent; import android.view.inputmethod.InputMethodManager; import android.widget.EditText;/*** 數(shù)字輸入,暫時(shí)不支持字母輸入所以把軟鍵盤全部屏蔽* * @author jwzhangjie*/ public class DigitsEditText extends EditText {public DigitsEditText(Context context) {super(context);init();}public DigitsEditText(Context context, AttributeSet attrs) {super(context, attrs);init();}public DigitsEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}private void init() {//設(shè)置一行顯示this.setInputType(InputType.TYPE_NULL);}@Overridepublic boolean onTouchEvent(MotionEvent event) {final boolean ret = super.onTouchEvent(event);// Must be done after super.onTouchEvent()applyKeyboardShowHide();return ret;}@Overridepublic void sendAccessibilityEventUnchecked(AccessibilityEvent event) {if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) {// Since we're replacing the text every time we add or remove a// character, only read the difference. (issue 5337550)final int added = event.getAddedCount();final int removed = event.getRemovedCount();final int length = event.getBeforeText().length();if (added > removed) {event.setRemovedCount(0);event.setAddedCount(1);event.setFromIndex(length);} else if (removed > added) {event.setRemovedCount(1);event.setAddedCount(0);event.setFromIndex(length - 1);} else {return;}} else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) {// The parent EditText class lets tts read "edit box" when this View// has a focus, which// confuses users on app launch (issue 5275935).return;}super.sendAccessibilityEventUnchecked(event);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {super.onLayout(changed, left, top, right, bottom);// Here we ensure that we hide the keyboard// Since this will be fired when virtual keyboard this will probably// blink but for now no better way were found to hide keyboard for sureapplyKeyboardShowHide();}@Overrideprotected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {super.onFocusChanged(focused, direction, previouslyFocusedRect);if (focused) {applyKeyboardShowHide();} else {final InputMethodManager imm = ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE));if (imm != null && imm.isActive(this)) {imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);}}}private void applyKeyboardShowHide() {final InputMethodManager imm = ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE));if (imm != null) {if (imm.isActive(this)) {imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);}}}}?
轉(zhuǎn)載于:https://www.cnblogs.com/pangblog/p/3395460.html
總結(jié)
以上是生活随笔為你收集整理的pjsip视频通信开发(上层应用)之EditText重写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【思考】一次交付项目小结
- 下一篇: proj1088