2012-12-26 51 views
19

我目前正在構建一個android應用程序,使用戶可以拍照並編寫它的詳細信息。該應用程序使用Sherlock庫。編輯文本和軟鍵盤在片段中的問題

我已經實現了一個SherlockFragment來顯示圖像和一些EditText和TextView以使用戶能夠輸入圖像的信息。但是,如果其中一個EditText處於焦點位置,則所有字段都將被推高,並彈出軟鍵盤。

這裏是我的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="200dip" 
     android:layout_above="@+id/textView" 
     android:layout_marginTop="20dip" 
     android:layout_marginBottom="20dip" 
     android:contentDescription="desc" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title:" 
     android:textStyle="bold" 
     android:layout_above="@+id/editTextSimple" 
     android:layout_marginBottom="2dip" 
     android:textSize="15sp"/> 

    <EditText 
     android:id="@+id/editTextSimple" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView1" 
     android:layout_marginBottom="4dip"> 
     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Description:" 
     android:textStyle="bold" 
     android:layout_above="@+id/editTextSimple1" 
     android:layout_marginBottom="2dip" 
     android:textSize="15sp"/> 

    <EditText 
     android:id="@+id/editTextSimple1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView2" 
     android:layout_marginBottom="12dip"> 
    </EditText> 

    <Button android:textColor="#FF000000" 
     android:id="@+id/submitButton" 
     android:text="@string/memorySubmit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="24dip" 
     android:onClick = "submit"/> 
</RelativeLayout> 

據我瞭解,在活動中,一個必須把這個<activity android:windowSoftInputMode="...." >在Android清單調整視圖時,軟鍵盤被要求。但是,我們如何設置這個碎片?

回答

9

的一個Activity所有片段具有相同的行爲,他們的父母Activity,但如果你需要爲不同Fragments不同的鍵盤行爲,可以動態地這個屬性從Fragment代碼改變這樣的:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.softInputMode.SOFT_INPUT_ADJUST_PAN); 

希望這有助於!

+0

嗨Evos!謝謝您的回答!不幸的是,它仍然無法正常工作。我認爲這個問題可能是因爲'android:layout_above',儘管我不能太確定它。 – user1140240

+0

你試過在這個參數中設置不同的變體嗎? – Evos

+1

我做到了。我意識到問題在於佈局本身,而不是軟鍵盤。所以我實現了一個滾動視圖,它的工作原理!還是要謝謝你的幫助。 – user1140240

51

這在我使用多個佈局和viewstub創建消息屏幕的場景中適用於我。把它放在onCreate()希望這有助於。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
+1

爲我工作,並保存我day.adding評論,以便其他人可能知道這個作品。 – dmSherazi

+1

@ kenodoggy這對我來說雖然是第一次打開,但當我點擊並開始輸入文本,然後我的佈局再次推高。如何解決這個問題?如果你知道,請告訴我。 – siva

+0

這就是我一直在尋找的東西。從片段 –

2

試試這個getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

8

這可以在片段類可用於移動編輯文本並滾動,直到結束。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 
1

我也這個問題發生在我的項目,我已經開發利用碎片和標籤我的項目,

如果我們採取相對佈局及以上保持和下方的依賴,並在與最後一個按鈕

機器人:layout_alignParentBottom =「真」

話反而會使得問題,解決的辦法是採取了滾動根直線或Rel內ative Layout和ScrollView內部將一個子視圖視爲Linear或Relative,並將所有視圖設置爲在RelativeLayout中相互位於下面(如果在子視圖中採用Relative)或不需要在Linear中設置依賴關係。

就是這樣,它會工作... :)

-1
android:launchMode="singleTask" 
    android:windowSoftInputMode="adjustPan" 

,如果你想改變這個清單中正常工作對我相關的行爲同樣是活動

+0

這將工作的活動,但他正在尋找如何用戶碎片..... – NagarjunaReddy

1
android:windowSoftInputMode="adjustPan" 

所有碎片,以改變相關碎片的行爲比使用以下一個

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);