當前位置:首頁 » 安卓系統 » android監聽鍵盤彈出

android監聽鍵盤彈出

發布時間: 2023-05-22 00:49:08

㈠ Android 鍵盤頂起最終完美解決方法

1.在布局底部放置一個透明的 view_Bottom,通隱沒陸過監聽鍵盤彈出修改 view_Bottom的高度實現,鍵盤彈出頂察碼起,鍵盤消失收回 的效果

4.想要不再界面一出現就彈出鍵盤,完善用戶體驗,只要在根布局裡加入灶頃,就能完美解決

Demo連接

㈡ android沒辦法監聽軟鍵盤的彈出和消失嗎

如果要監聽,目簡型啟前Android沒有提供監聽的callback的注租亮冊介面,要完攔如美實現這個可能需要改framework。

㈢ android的鍵盤彈出和縮回應該怎麼處理

監聽布局的高度來判斷軟鍵盤的打開和關閉public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {

public interface SoftKeyboardStateListener {
void onSoftKeyboardOpened(int keyboardHeightInPx);
void onSoftKeyboardClosed();
}

private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
private final View activityRootView;
private int lastSoftKeyboardHeightInPx;
private boolean isSoftKeyboardOpened;

public SoftKeyboardStateHelper(View activityRootView) {
this(activityRootView, false);
}

public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
this.activityRootView = activityRootView;
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}

@Override
public void onGlobalLayout() {
final Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);

final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
isSoftKeyboardOpened = true;
notifyOnSoftKeyboardOpened(heightDiff);
} else if (isSoftKeyboardOpened && heightDiff < 100) {
isSoftKeyboardOpened = false;
notifyOnSoftKeyboardClosed();
}
}

public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
}

public boolean isSoftKeyboardOpened() {
return isSoftKeyboardOpened;
}

/**
* Default value is zero (0)
* @return last saved keyboard height in px
*/
public int getLastSoftKeyboardHeightInPx() {
return lastSoftKeyboardHeightInPx;
}

public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
listeners.add(listener);
}

public void (SoftKeyboardStateListener listener) {
listeners.remove(listener);
}

private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardOpened(keyboardHeightInPx);
}
}
}

private void notifyOnSoftKeyboardClosed() {
for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardClosed();
}
}
}
}

㈣ H5如何對android和ios手機軟鍵盤的監聽

    總結:

    1.在ios中軟鍵盤彈起時,僅會引起body的scrollTop值改變,但是我們可以通過輸入框的獲取焦點情況來做判斷,單也只能在ios中採用這個方案,因為在android中存在主動受氣鍵盤後,但輸入框並沒有失去焦點,而ios中鍵盤受氣後就會失去焦點;

2.在androis中軟鍵盤彈起或收起時,會改變window的高度,因此監聽window的onresize事件;

一、Android

//獲取原窗口的高度

var originalHeight=document.documentElement.clientHeight ||document.body.clientHeight;

window.onresize=function(){

    //鍵盤彈起與隱藏都會引起窗口的高度發生變化

      var resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;

        if(resizeHeight-0<originalHeight-0){

        //當軟鍵盤彈起,在此處操作

        }else{

        //當軟鍵盤收起,在此處操作

        }

}

二、ios

focusin和focusout支持冒泡,對應focus和blur, 使用focusin和focusout的原因是focusin和focusout可以冒泡,focus和blur不會冒泡,這樣就可以使用事件代理,處理多個輸入框存在的情況。

document.body.addEventListener('focusin', () => {

            //軟鍵盤彈出的事件處理

            if(isIphone()){

            }

        })

  document.body.addEventListener('focusout', () => {

      //軟鍵盤收起的事件處理

        if(isIphone()){

        }

  })

特此聲明文章出處: https://blog.csdn.net/u012982629/article/details/81905894

㈤ Android軟鍵盤彈出和收回監聽

軟鍵盤彈出和收回的三種方式

為什麼 重寫 Activity.onKeyDown() 方法為什麼沒有用?而用的是 onKeyPreIme,該博客寫的很明白 http://blog.csdn.net/yxhuang2008/article/details/53822072 ,感謝該博客博主

軟鍵盤收回監聽

//鍵盤隱藏監聽
customized_edit.setOnKeyBoardHideListener(new TextEditTextView.OnKeyBoardHideListener(){

});

軟鍵盤彈出監聽(通過高度計算,但是這種方式有問題,不推薦用):

㈥ android 判斷鍵盤彈出和消失的監聽事件的方法

原理:自定義布局的onSizeChanged()方法,在其中增加一個監聽介面,當軟鍵盤顯示或隱藏使得布局尺寸發生改變,就能捕獲到這個事件。

具體方法如下:
首先,在清單文件Manifest.xml中的對應的Activity標簽內設置
android:windowSoftInputMode=」adjustResize」
作用是當軟鍵盤顯示或隱藏時,該Activity主窗口總是會被調整大小以便留出軟鍵盤的空間。唯有這樣才能保證布局觸發onSizeChanged()方法。

然後,自定義一個布局,具體是RelativeLayout、LinearLayout或是其它的,根據實際情況而定,自定義的目的是在其onSizeChanged()方法中增加一個監聽介面。這里給出一個自定義RelativeLayout布局代碼:

public class CustomRelativeLayout extends RelativeLayout {

private OnSizeChangedListener listener;

public CustomRelativeLayout(Context context) {
super(context);
}

public CustomRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

Log.d(TAG.CUSTOM_VIEW, 「onSizeChanged」);
super.onSizeChanged(w, h, oldw, oldh);
if (listener != null) {
listener.onSizeChanged(w, h, oldw, oldh);
}
}

public void setOnSizeChangedListener(OnSizeChangedListener listener) {
this.listener = listener;
}

/** * Activity主窗口大小改變時的回調介面(本示例中,等價於軟鍵盤顯示隱藏時的回調介面) */
public interface OnSizeChangedListener {
public void onSizeChanged(int w, int h, int oldw, int oldh);
}
}

最後,在程序中使用此介面(xxx.setOnSizeChangedListener(…))即可實現監聽鍵盤隱藏或顯示事件。

㈦ 如何在android中判斷軟鍵盤是否彈出或隱藏

Android中判斷軟鍵盤是否彈出或隱藏可以借悉棚助軟鍵盤顯示和隱藏時,對主窗口進行了重新布局這個特性來進行偵聽。如果我們設置的模式為壓縮模式,那麼我們可以對布局的onSizeChanged函數進行跟蹤,如果為平移模式,那麼該函數可能不會被調用。
假設跟布局為線性布局耐橘,模式為壓縮模式,我們寫一個例子,當輸入昌陸團法彈出時隱藏某個view,輸入法隱藏時顯示某個view。

public class ResizeLayout extends LinearLayout{
private OnResizeListener mListener;
public interface OnResizeListener {
void OnResize(int w, int h, int oldw, int oldh);
}
public void setOnResizeListener(OnResizeListener l) {
mListener = l;
}
public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh);
if (mListener != null) {
mListener.OnResize(w, h, oldw, oldh);
}
}
}

㈧ android 如何讓軟鍵盤自動彈出

可以通過下面的代碼來實現Android自動彈出軟鍵盤:

edittext.requestFocus(); //edittext是一個EditText控制項
Timer timer = new Timer(); //設置定時器
timer.schele(new TimerTask() {
@Override
public void run() { //彈出軟鍵盤的代碼
 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.showSoftInput(edittext, InputMethodManager.RESULT_SHOWN);
 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 300); //設置300毫秒的時長

說明:同樣是在寫Techo Lite時遇到的問題。希望程序可以自動彈出軟鍵盤,可是嘗試了各種辦法都沒有效果。問題的原因似乎是必須等待UI繪制完成後彈出軟鍵盤的代碼才會有效。於是就採用了上面的方法。需要注意的是,在此之前必須讓edittext獲取焦點,不然也是無效的。

㈨ 如何在Android中判斷軟鍵盤是否彈出或隱藏

不知道你是州氏侍說的哪一種?
(1):虛擬鍵盤彈出後一般會占據當前activity的核旦大部分,所以肉冊吵眼可以看到;
(2):代碼中判斷:
//判斷隱藏軟鍵盤是否彈出
if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParamsSOFT_INPUT_STATE_UNSPECIFIED)
{
//隱藏軟鍵盤
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

㈩ 如何監聽軟鍵盤的彈出和退出

Android是一個針對觸摸屏專門設計的操作系統,當點擊編輯框,系統自動為用戶彈出軟鍵盤,以便用戶進行輸入。
那麼,彈出軟鍵盤後必然會造成原有布局高度的減少,那麼系統應該如何來處理布局的減少?我們能否在應用程序中進行自定義的控制?這些是本文要討論的重點。

一、軟鍵盤顯示的原理
軟體盤的本質是什麼?軟鍵盤其實是一個Dialog!
InputMethodService為我們的輸入法創建了一個Dialog,並且將該Dialog的Window的某些參數(如Gravity)進行了設置,使之能夠在底部或者全屏顯示。當我們點擊輸入框時,系統對活動主窗口進行調整,從而為輸入法騰出相應的空間,然後將該Dialog顯示在底部,或者全屏顯示。
二、活動主窗口調整
android定義了一個屬性,名字為windowSoftInputMode, 用它可以讓程序可以控制活動主窗口調整的方式。我們可以在AndroidManifet.xml中對Activity進行設置。如:android:windowSoftInputMode="stateUnchanged|adjustPan"
該屬性可選的值有兩部分,一部分為軟鍵盤的狀態控制,另一部分是活動主窗口的調整。前一部分本文不做討論,請讀者自行查閱android文檔。
模式一,壓縮模式
windowSoftInputMode的值如果設置為adjustResize,那麼該Activity主窗口總是被調整大小以便留出軟鍵盤的空間。
我們通過一段代碼來測試一下,當我們設置了該屬性後,彈出輸入法時,系統做了什麼。
重寫Layout布局:

1. public class ResizeLayout extends LinearLayout{
2. private static int count = 0;
3.
4. public ResizeLayout(Context context, AttributeSet attrs) {
5. super(context, attrs);
6. }
7.
8. @Override
9. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
10. super.onSizeChanged(w, h, oldw, oldh);
11.
12. Log.e("onSizeChanged " + count++, "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh);
13. }
14.
15. @Override
16. protected void onLayout(boolean changed, int l, int t, int r, int b) {
17. super.onLayout(changed, l, t, r, b);
18. Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b="+b);
19. }
20.
21. @Override
22. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
23. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
24.
25. Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);
26. }

我們的布局設置為:

1. <com.winuxxan.inputMethodTest.ResizeLayout
2. xmlns:android="http://schemas.android.com/apk/res/android"
3. android:id="@+id/root_layout"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:orientation="vertical"
7. >
8.
9. <EditText
10. android:layout_width="fill_parent"
11. android:layout_height="wrap_content"
12. />
13.
14. <LinearLayout
15. android:id="@+id/bottom_layout"
16. android:layout_width="fill_parent"
17. android:layout_height="fill_parent"
18. android:orientation="vertical"
19. android:gravity="bottom">s
20.
21. <TextView
22. android:layout_width="fill_parent"
23. android:layout_height="wrap_content"
24. android:text="@string/hello"
25. android:background="#77777777"
26. />
27. </LinearLayout>
28. </com.winuxxan.inputMethodTest.ResizeLayout>

AndroidManifest.xml的Activity設置屬性:android:windowSoftInputMode = "adjustResize"
運行程序,點擊文本框,查看調試信息:
E/onMeasure 6(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742024
E/onMeasure 7(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742025
E/onSizeChanged 8(7960): =>onSizeChanged called! w=320,h=201,oldw=320,oldh=377
E/onLayout 9(7960): =>OnLayout called! l=0, t=0,r=320,b=201
從調試結果我們可以看出,當我們點擊文本框後,根布局調用了onMeasure,onSizeChanged和onLayout。
實際上,當設置為adjustResize後,軟鍵盤彈出時,要對主窗口布局重新進行measure和layout,而在layout時,發現窗口的大小發生的變化,因此調用了onSizeChanged。
從下圖的運行結果我們也可以看出,原本在下方的TextView被頂到了輸入法的上方。

模式二,平移模式
windowSoftInputMode的值如果設置為adjustPan,那麼該Activity主窗口並不調整屏幕的大小以便留出軟鍵盤的空間。相反,當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為用戶可能關閉軟鍵盤以便獲得與被覆蓋內容的交互操作。
上面的例子中,我們將AndroidManifest.xml的屬性進行更改:android: windowSoftInputMode = "adjustPan"

重新運行,並點擊文本框,查看調試信息:
E/onMeasure 6(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742200
E/onMeasure 7(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742201
E/onLayout 8(8378): =>OnLayout called! l=0, t=0,r=320,b=377
我們看到:系統也重新進行了measrue和layout,但是我們發現,layout過程中onSizeChanged並沒有調用,這說明輸入法彈出前後並沒有改變原有布局的大小。
從下圖的運行結果我們可以看到,下方的TextView並沒有被頂到輸入法上方。

事實上,當輸入框不會被遮擋時,該模式沒有對布局進行調整,然而當輸入框將要被遮擋時,窗口就會進行平移。也就是說,該模式始終是保持輸入框為可見。如下圖,整個窗口,包括標題欄均被上移,以保證文本框可見。

模式三 自動模式
當屬性windowSoftInputMode被設置為adjustUspecified時,它不被指定是否該Activity主窗口調整大小以便留出軟鍵盤的空間,或是否窗口上的內容得到屏幕上當前的焦點是可見的。系統將自動選擇這些模式中一種主要依賴於是否窗口的內容有任何布局視圖能夠滾動他們的內容。如果有這樣的一個視圖,這個窗口將調整大小,這樣的假設可以使滾動窗口的內容在一個較小的區域中可見的。這個是主窗口默認的行為設置。
也就是說,系統自動決定是採用平移模式還是壓縮模式,決定因素在於內容是否可以滾動。

三、偵聽軟鍵盤的顯示隱藏
有時候,藉助系統本身的機制來實現主窗口的調整並非我們想要的結果,我們可能希望在軟鍵盤顯示隱藏的時候,手動的對布局進行修改,以便使軟鍵盤彈出時更加美觀。這時就需要對軟鍵盤的顯示隱藏進行偵聽。
直接對軟鍵盤的顯示隱藏偵聽的方法本人沒有找到,如果哪位找到的方法請務必告訴本人一聲。還有本方法針對壓縮模式,平移模式不一定有效。
我們可以藉助軟鍵盤顯示和隱藏時,對主窗口進行了重新布局這個特性來進行偵聽。如果我們設置的模式為壓縮模式,那麼我們可以對布局的onSizeChanged函數進行跟蹤,如果為平移模式,那麼該函數可能不會被調用。
我們可以重寫根布局,因為根布局的高度一般情況下是不發生變化的。
假設跟布局為線性布局,模式為壓縮模式,我們寫一個例子,當輸入法彈出時隱藏某個view,輸入法隱藏時顯示某個view。

1. public class ResizeLayout extends LinearLayout{
2. private OnResizeListener mListener;
3.
4. public interface OnResizeListener {
5. void OnResize(int w, int h, int oldw, int oldh);
6. }
7.
8. public void setOnResizeListener(OnResizeListener l) {
9. mListener = l;
10. }
11.
12. public ResizeLayout(Context context, AttributeSet attrs) {
13. super(context, attrs);
14. }
15.
16. @Override
17. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
18. super.onSizeChanged(w, h, oldw, oldh);
19.
20. if (mListener != null) {
21. mListener.OnResize(w, h, oldw, oldh);
22. }
23. }
24. }

在我們的Activity中,通過如下方法調用:

1. public class InputMethodTestActivity extends Activity {
2. private static final int BIGGER = 1;
3. private static final int SMALLER = 2;
4. private static final int MSG_RESIZE = 1;
5.
6. private static final int HEIGHT_THREADHOLD = 30;
7.
8. class InputHandler extends Handler {
9. @Override
10. public void handleMessage(Message msg) {
11. switch (msg.what) {
12. case MSG_RESIZE: {
13. if (msg.arg1 == BIGGER) {
14. findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
15. } else {
16. findViewById(R.id.bottom_layout).setVisibility(View.GONE);
17. }
18. }
19. break;
20.
21. default:
22. break;
23. }
24. super.handleMessage(msg);
25. }
26. }
27.
28. private InputHandler mHandler = new InputHandler();
29.
30. /** Called when the activity is first created. */
31. @Override
32. public void onCreate(Bundle savedInstanceState) {
33. super.onCreate(savedInstanceState);
34. setContentView(R.layout.main);
35.
36. ResizeLayout layout = (ResizeLayout) findViewById(R.id.root_layout);
37. layout.setOnResizeListener(new ResizeLayout.OnResizeListener() {
38.
39. public void OnResize(int w, int h, int oldw, int oldh) {
40. int change = BIGGER;
41. if (h < oldh) {
42. change = SMALLER;
43. }
44.
45. Message msg = new Message();
46. msg.what = 1;
47. msg.arg1 = change;
48. mHandler.sendMessage(msg);
49. }
50. });
51. }
52. }

這里特別需要注意的是,不能直接在OnResizeListener中對要改變的View進行更改,因為OnSizeChanged函數實際上是運行在 View的layout方法中,如果直接在onSizeChange中改變view的顯示屬性,那麼很可能需要重新調用layout方法才能顯示正確。然而我們的方法又是在layout中調用的,因此會出現錯誤。因此我們在例子中採用了Handler的方法。

熱點內容
sqlserver標識 發布:2025-02-12 23:51:33 瀏覽:462
安卓怎麼玩地牢獵人 發布:2025-02-12 23:50:25 瀏覽:943
思鄉腳本 發布:2025-02-12 23:43:32 瀏覽:439
java的job 發布:2025-02-12 23:38:43 瀏覽:892
我的世界伺服器授權指令 發布:2025-02-12 23:30:13 瀏覽:596
電腦伺服器號在哪裡找 發布:2025-02-12 23:22:29 瀏覽:12
linux查看系統是32位 發布:2025-02-12 23:17:29 瀏覽:989
從資料庫中隨機取資料庫數據 發布:2025-02-12 23:17:25 瀏覽:878
ftp下載軟體安卓 發布:2025-02-12 23:07:24 瀏覽:567
c搜索演算法 發布:2025-02-12 23:05:47 瀏覽:862