2014-10-20 61 views
2

我禁用正常頂部操作欄通過使用不隱藏字段:的Android底部的操作欄時使用軟鍵盤

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 

&我想用一個底部的動作條來完成的/取消這樣的日曆應用程序操作: enter image description here

但是當我嘗試寫一些東西在滾動視圖中可用的editTexts,底部操作欄隱藏字段,&我希望它像也低於日曆應用程序可見: enter image description here

那麼,我該如何實現類似的行爲呢?(所以打開軟鍵盤時,底部操作欄不會隱藏任何領域),

我用這樣的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:baselineAligned="true" 
android:orientation="vertical" 
android:weightSum="1"> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="false" 
    android:id="@+id/formScrollView"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <!-- all form fields goes here --> 
    </LinearLayout> 

</ScrollView> 

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:padding="@dimen/done_button_padding" 
    android:id="@+id/happeningDoneLayout"> 

    <Button 
     android:id="@+id/doneButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="@string/done" 
     android:layout_alignParentBottom="true"/> 
    <Button 
     android:id="@+id/cancelButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="@string/cancel" 
     android:layout_alignParentBottom="true"/> 
</FrameLayout> 

</RelativeLayout> 

回答

1

爲確保底部操作欄不會隱藏任何其他控件,ScrollView和欄可以以垂直線性佈局進行堆疊。這樣,當鍵盤出現/消失時,ScrollView可以在焦點控件可見的情況下收縮/展開,同時保持ScrollView下方屏幕底部始終可見的底部操作欄。

adjustPan不應該與此解決方案一起使用。

權重是分佈式的,ScrollView是動態改變其高度的部分。

下面的代碼的最小化樣本:

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


     <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/formScrollView" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      ... > 

       ... 

     </ScrollView> 

     <FrameLayout 
      android:id="@+id/bottomBar" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      ... > 

       ... 

     </FrameLayout> 

    </LinearLayout> 
1

Android Keyboard hides EditText

機器人:windowSoftInputMode =「adjustPan | adjustResize「

這應該會給你你想要的效果。把它放在相關活動的清單上。做

+0

但這恰恰是沒有問題的,我需要類似問題的日曆應用程序行爲,所以底部欄仍然會高於軟可見鍵盤,但會推動editText比平常更遠一點,以便在酒吧上方可見 – AbdelHady 2014-10-20 13:45:40

2

最簡單的辦法是防止你的佈局被調整時,虛擬鍵盤出現:

<activity 
     android:name=".ShareFromDriveActivity_" 
     android:hardwareAccelerated="true" 
     android:label="@string/titleSharingCalendar" 
     android:launchMode="standard" 
     android:parentActivityName=".AppWidgetConfigure_" 
     android:screenOrientation="sensor" 
     android:theme="@style/Theme.Materialamberpurple" 
     android:windowSoftInputMode="stateHidden|adjustPan" > 
     <intent-filter> 
      <action android:name="de.kashban.android.picturecalendar.INTENT_ACTION_SHARE_FROM_DRIVE" /> 
     </intent-filter> 
    </activity> 

的重要行是機器人:windowSoftInputMode =「stateHidden | adjustPan」stateHidden確保在開始活動時鍵盤不會打開,即使EditText具有焦點。

adjustPan您正在尋找的是:佈局將不再調整大小(包括您的下部按鈕),但鍵控將覆蓋佈局。它仍然可以滾動到可見部分,但是當鍵盤出現時,它們不可見。

來源:Android Guides

也許僅此設置會幫助你的情況。

如果這還不夠,你需要的按鈕要真的走了,請嘗試使用此:

// Detect soft keyboard visibility changes 
final SoftKeyboardStateHelper softKeyboardStateHelper = 
     new SoftKeyboardStateHelper(lyt_share_from_drive_main); 
softKeyboardStateHelper.addSoftKeyboardStateListener(this); 

SoftKeyboardStateHelper是阿爾喬姆Zinnatullin一個類檢測Softkeyboard的狀態變化:

/** 
* 
*/ 
package de.kashban.android.picturecalendar.util.local; 

/** 
* @author Artem Zinnatullin 
* http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android/9108219#9108219 
* Usage: final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout); 
*  softKeyboardStateHelper.addSoftKeyboardStateListener(...); 
*/ 
import android.graphics.Rect; 
import android.view.View; 
import android.view.ViewTreeObserver; 

import java.util.LinkedList; 
import java.util.List; 

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 removeSoftKeyboardStateListener(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(); 
      } 
     } 
    } 
} 

在您的活動中實施界面SoftKeyboardStateListener並覆蓋這些方法:

@Override 
public void onSoftKeyboardOpened(int keyboardHeightInPx) { 
    if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardOpened() called with keyboard height " + keyboardHeightInPx); 
    rdgVisibility.setVisibility(View.GONE); 
    if (tvPermissionLabel != null) 
     tvPermissionLabel.setVisibility(View.GONE); 
    lyt_ShareDriveOkCancel.setVisibility(View.GONE); 
    cbShareWithDev.setVisibility(View.GONE); 

} 

@Override 
public void onSoftKeyboardClosed() { 
    if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardClosed() called."); 
    rdgVisibility.setVisibility(View.VISIBLE); 
    if (tvPermissionLabel != null) 
     tvPermissionLabel.setVisibility(View.VISIBLE); 
    lyt_ShareDriveOkCancel.setVisibility(View.VISIBLE); 
    cbShareWithDev.setVisibility(View.VISIBLE); 
} 

在這兩種方法中相應地更改下部按鈕的可見性。完成。

這裏是它的外觀在我的應用程序:

Full Dialog Screenshot

鍵盤被關閉,全力佈局可見

Most Controls hidden Screenshot

鍵盤是開放的,所有的控制,但EditText上了。原因是EditText可能會跨越多行,而在小屏幕上,整個佈局已經很混亂。