簡體   English   中英

出現鍵盤時Android調整視圖

[英]Android adjust view when keyboard appears

我想創建一個動態登錄屏幕,當屏幕足夠高但不包括短屏幕上的圖像時,其中包含裝飾性品牌圖像。 當鍵盤出現時,很可能需要刪除圖像。 當鍵盤隱藏時,圖像可以回來。

對於網頁,我只需要對設備高度使用 CSS 媒體查詢,適當地顯示或隱藏圖像,一切都會很好地工作。 我認為 Android 視圖不可能有這么簡單和干凈的東西,是嗎? 所以我想我需要知道創建活動時窗口的高度並適當地創建視圖。

在清單中,我已經把我的主要活動,以adjustResize鍵盤出現時。 當鍵盤出現時,我的視圖會調整大小,但令人驚訝的是我的活動沒有重新創建。 當屏幕旋轉時,活動被重新創建。

該文檔說,當鍵盤可用性發生變化時,將重新創建視圖。 https://developer.android.com/guide/topics/resources/runtime-changes的第一段

某些設備配置可能會在運行時更改(例如屏幕方向、鍵盤可用性以及用戶啟用多窗口模式時)。 當發生這種變化時,Android 會重新啟動正在運行的 Activity(調用 onDestroy(),然后調用 onCreate())。 重啟行為旨在通過使用與新設備配置匹配的替代資源自動重新加載您的應用程序,從而幫助您的應用程序適應新配置。

我的問題是

處理我的設計目標的最佳方式是什么?

為什么出現鍵盤時沒有重新創建我的活動?

以下是我的測試應用程序的相關部分。 這里沒有圖像,因為在遇到與文檔相矛盾的行為之前,我什至沒有走那么遠。

AndroidManifest.xml

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

主活動.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("MainActivity", "onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

活動_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Top"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorAccent" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/intro"
        android:singleLine="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Bottom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@color/colorAccent" />

</android.support.constraint.ConstraintLayout>

出現鍵盤時不會重新創建活動,因為這不是Android的工作方式。 它不應該是。 (盡管如果您有帶滑出式物理鍵盤的老式設備,則將其滑出時會重新創建,因為它被視為硬件配置更改)。 顯示/隱藏的鍵盤無需重新創建。 一件好事是,鑒於有多少人只是將大量邏輯推入onCreate中,所以許多重新創建事件將是昂貴的。

如何做你想做的-你做不到。 沒有API可以檢測何時打開鍵盤。 有一些常用的駭客試圖發現它,但是它們都有缺陷(它們可能在分屏模式,畫中畫模式,多屏幕以及鍵盤太小的情況下會出現問題,因為它們都基於猜測而工作。高度變化)。

每當View獲得或失去焦點時,方法OnFocusChangeListener()都會檢測到。 EditText使鍵盤一獲得焦點就會顯示出來。 因此,您應該將OnFocusChangeListener()附加到所需的那些EditText

EditText myET = (EditText)findViewById(R.id.myET);

myET.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override          
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
            //Hide image
        } else {
            //Reveal image
        }
    }
});

此外,要隱藏和顯示圖像,您應該使用稱為visibility的屬性。 方法是:

myImageView.setVisibility(View.VISIBLE); //This will show the Image
myImageView.setVisibility(View.INVISIBLE); //This will make the Image invisible
myImageView.setVisibility(View.GONE); //This will make the Image invisible, and also it will collapse the layout, occupying no space at all.

不要嘗試同時使用INVISIBLEGONE ,因為這可能會造成一些麻煩。 就您而言,據我了解,您可能想使用GONEVISIBLE

這種方法的唯一問題是,如果您有多個EditText,則必須多次設置相同的代碼。 若要解決此問題,請參考以下鏈接: 所有EditText上的EditText setOnFocusChangeListener

我不是專業人士,但也有類似的問題。 經過數小時的瀏覽,這是我的處理方式。 1.設置軟鍵盤外觀偵聽器。2.在相應的if子句中將imageView設置為View.VISIBLE和View.GONE。

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    Log.d(TAG, "keypadHeight = " + keypadHeight);

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
        //set image to View.GONE
    }
    else {
        // keyboard is closed
       //set image to View.VISIBLE
    }
}
});

我一直在尋找答案,並且遇到了這個問題:

https://developer.android.com/training/keyboard-input/visibility#java

它說android does做你想要的。

我剛剛嘗試過,將1行ONE LINE!添加到您的清單中,它可以正常工作。

哦,等等...您希望品牌消失...嗯,這不是必須的嗎?

DOH! 您已經知道了.....

“你想做的你不能做”……我不知道該怎么做,但是我不同意它不能完成。 我有PayPal應用程序,當鍵盤出現登錄按鈕時,登錄屏幕的其余部分將調整大小,以便鍵盤不會覆蓋登錄按鈕。 我不知道他們的操作方式,但顯然該應用程序以某種方式知道鍵盤的出現並進行相應的調整。

我一直在尋找答案,並且遇到了這個問題:

https://developer.android.com/training/keyboard-input/visibility#java

它說,當軟鍵盤出現/消失時,Android會調整屏幕大小。

我剛剛嘗試過,將1行ONE LINE!添加到您的清單中,它可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM