2011-11-03 59 views
6

我有一個EditText(用戶可以鍵入號碼), ,所以當用戶點擊EditText文本框時,打開一個帶數字的鍵盤。Android:鍵盤與EditText重疊(與打印屏幕)

This is how it looks when the text box is clicked

,你可以看到鍵盤隱藏文本框的一小部分。

但是,當我按下一個鍵,例如0,它看起來沒問題。 This is how it looks after after clicking 0

有什麼我可以做的(除了把EditText設置得更高),所以它會看起來像它在第二張圖片嗎?

編輯:將.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:weightSum="1"> 
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true"> 
     <android.widget.CheckedTextView android:id="@+id/checkedTextView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp" android:text="@string/toString"></android.widget.CheckedTextView> 
     <AutoCompleteTextView android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent" android:text="@string/emptyString" android:textSize="17sp" android:gravity="top|left" android:minHeight="62dp"> 
      <requestFocus></requestFocus> 
     </AutoCompleteTextView> 
     <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2"> 
      <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.33333333333" android:text="@string/contactsString" android:textSize="17sp" android:id="@+id/contactsButton"></Button> 
      <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/groupsString" android:layout_width="fill_parent" android:id="@+id/groupsButton" android:textSize="17sp"></Button> 
      <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/favouritesString" android:layout_width="fill_parent" android:id="@+id/button3" android:textSize="17sp"></Button> 
     </LinearLayout> 
     <TextView android:id="@+id/textView1" android:text="@string/messageString" android:layout_height="wrap_content" android:textSize="17sp" android:layout_width="fill_parent"></TextView> 
     <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_width="fill_parent" android:gravity="top|left" android:minHeight="105dp"></EditText> 
     <TextView android:id="@+id/textView2" android:text="@string/repetition" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp"></TextView> 
     <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner> 
     <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="fill_parent"> 
      <ImageView android:src="@drawable/button_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_weight="0.1"></ImageView> 
      <EditText android:layout_height="wrap_content" android:id="@+id/timeET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4"></EditText> 
      <ImageView android:src="@drawable/button_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView2" android:layout_weight="0.1"></ImageView> 
      <EditText android:layout_height="wrap_content" android:id="@+id/dateET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText> 
     </LinearLayout> 
     <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentLeft="true"> 
       <Button android:layout_weight="0.5" android:layout_height="wrap_content" android:text="@string/button_ok" android:layout_width="fill_parent" android:id="@+id/button4" android:textSize="17sp"></Button> 
       <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/button5" android:layout_weight="0.5" android:text="@string/button_cancel" android:textSize="17sp"></Button> 
      </LinearLayout> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 

</LinearLayout> 

回答

18

我試過你的XML,是的,你是正確的問題發生。

爲了解決這個問題,我在MainActivity.java中寫了這行,希望對你有所幫助,並將佈局XML放在ScrollView中。

活動

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.temp); 
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

     final EditText time = (EditText)findViewById(R.id.timeET); 
     time.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       time.requestLayout(); 
       MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED); 

       return false; 
      } 
     }); 
     final EditText date = (EditText)findViewById(R.id.dateET); 
     date.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       time.requestLayout(); 
       MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED); 

       return false; 
      } 
     }); 
     } 

XML是什麼樣子,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

     <ScrollView android:id="@+id/scrollView1" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:weightSum="1"> 
--- 
--- 
--- 
     </ScrollView> 
</LinearLayout> 
+0

我會嘗試這個今天晚些時候或明天上午我會讓你知道這是如何工作的。謝謝! – Belgi

+0

p.s:你的意思是寫date.requestLayout();在 date.setOnTouchListener? – Belgi

+0

@Frankenstein嘿嘿,非常感謝你的評論。我想現在刪除我的答案,但您的評論阻止了我!無論如何,我大致回答我認爲根ScrollView將解決問題。似乎我錯了。爲什麼我們在你的答案中需要根LinearLayout?我對這個問題的細節並沒有太多的瞭解,但我認爲OnFocus聽衆是正確的選擇。無論如何,我無法評論細節,我有我的週末享受。再次感謝你 !! +1 –

2

你可以給一些提示就如何通過Android處理這個系統:windowSoftInputMode元素在AndroidManifest申報活動。嘗試「adjustResize」值。

android:windowSoftInputMode

+0

這完全隱藏的EditText文本框 – Belgi

2

設置在活動android:windowSoftInputMode"adjustPan"

不能調整大小活動的主窗口,以騰出空間給軟鍵盤。相反,窗口的內容會自動平移,以便當前焦點永遠不會被鍵盤遮擋,用戶始終可以看到他們正在鍵入的內容。

當將此技術用於全屏活動時,請謹防one potential bug

+0

不同的輸入模式,包括一個更加直觀的解釋上述建議 - 這應該做的花樣 - ,可以發現[點擊這裏](http://developer.android.com/resources/articles/on-screen-inputs.html)。 –

+0

看起來像第一張照片:/ – Belgi

3

把整個視圖放在一個ScrollView中,並設置android:windowSoftInputMode = adjustPan它會做的。

你只需要添加這段代碼,

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

Your linear layout here.... 

</ScrollView> 

我已經測試過它在我的HTC Desire,其工作正常,我希望它會爲你工作了。

+0

你能幫助我如何使佈局成爲ScrollView嗎? 我將在一秒內添加.xml代碼 – Belgi

+0

我將它更改爲ScrollView,但它看起來更加糟糕...... – Belgi

1

嘗試改變線性佈局滾動視圖......所以,如果鍵盤附帶上述editt文本用戶可以滾動和類型...

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:weightSum="1"> 
<RelativeLayout android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <LinearLayout android:layout_width="wrap_content" 
     android:orientation="vertical" android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1" android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true"> 
     <android.widget.CheckedTextView 
      android:id="@+id/checkedTextView1" android:layout_height="wrap_content" 
      android:layout_width="fill_parent" android:textSize="17sp"></android.widget.CheckedTextView> 
     <AutoCompleteTextView android:layout_height="wrap_content" 
      android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent" 
      android:textSize="17sp" android:gravity="top|left" 
      android:minHeight="62dp"> 
      <requestFocus></requestFocus> 
     </AutoCompleteTextView> 
     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@+id/linearLayout2"> 
      <Button android:layout_height="wrap_content" 
       android:layout_width="fill_parent" android:layout_weight="0.33333333333" 
       android:textSize="17sp" android:id="@+id/contactsButton"></Button> 
      <Button android:layout_weight="0.33333333333" 
       android:layout_height="wrap_content" android:layout_width="fill_parent" 
       android:id="@+id/groupsButton" android:textSize="17sp"></Button> 
      <Button android:layout_weight="0.33333333333" 
       android:layout_height="wrap_content" android:layout_width="fill_parent" 
       android:id="@+id/button3" android:textSize="17sp"></Button> 
     </LinearLayout> 
     <TextView android:id="@+id/textView1" android:layout_height="wrap_content" 
      android:textSize="17sp" android:layout_width="fill_parent"></TextView> 
     <EditText android:layout_height="wrap_content" android:id="@+id/editText1" 
      android:layout_width="fill_parent" android:gravity="top|left" 
      android:minHeight="105dp"></EditText> 
     <TextView android:id="@+id/textView2" android:layout_height="wrap_content" 
      android:layout_width="fill_parent" android:textSize="17sp"></TextView> 
     <Spinner android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner> 
     <LinearLayout android:layout_height="wrap_content" 
      android:id="@+id/linearLayout3" android:layout_width="fill_parent"> 
      <ImageView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:id="@+id/imageView1" 
       android:layout_weight="0.1"></ImageView> 
      <EditText android:layout_height="wrap_content" android:id="@+id/timeET" 
       android:inputType="number" android:layout_width="wrap_content" 
       android:layout_weight="0.4"></EditText> 
      <ImageView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:id="@+id/imageView2" 
       android:layout_weight="0.1"></ImageView> 
      <EditText android:layout_height="wrap_content" android:id="@+id/dateET" 
       android:inputType="number" android:layout_width="wrap_content" 
       android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText> 
     </LinearLayout> 
     <RelativeLayout android:id="@+id/relativeLayout2" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <LinearLayout android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:id="@+id/linearLayout4" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentLeft="true"> 
       <Button android:layout_weight="0.5" android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/button4" 
        android:textSize="17sp"></Button> 
       <Button android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/button5" 
        android:layout_weight="0.5" android:textSize="17sp"></Button> 
      </LinearLayout> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 

請necesscery變化......我已刪除的字符串和我convience..u繪製的SRC需要改變第一 線性佈局沒有這個 機器人到scrollview..try:windowSoftInputMode = adjustPan

+0

我將它更改爲ScrollView,但它看起來更糟糕...... – Belgi

+0

我編輯過......請在上面 – Rockin

5

更改爲滾動型以這樣的方式

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:weightSum="1" > 
     <RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <LinearLayout 
       android:layout_width="wrap_content" 
       android:orientation="vertical" 
       android:layout_height="wrap_content" 
       android:id="@+id/linearLayout1" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentRight="true" > 
       <android.widget.CheckedTextView 
        android:id="@+id/checkedTextView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:textSize="17sp" 
        android:text="@string/toString" /> 
       <AutoCompleteTextView 
        android:layout_height="wrap_content" 
        android:id="@+id/autoCompleteTextView1" 
        android:layout_width="fill_parent" 
        android:text="@string/emptyString" 
        android:textSize="17sp" 
        android:gravity="top|left" 
        android:minHeight="62dp" > 
        <requestFocus></requestFocus> 
       </AutoCompleteTextView> 
       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/linearLayout2" > 
        <Button 
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent" 
         android:layout_weight="0.33333333333" 
         android:text="@string/contactsString" 
         android:textSize="17sp" 
         android:id="@+id/contactsButton" /> 
        <Button 
         android:layout_weight="0.33333333333" 
         android:layout_height="wrap_content" 
         android:text="@string/groupsString" 
         android:layout_width="fill_parent" 
         android:id="@+id/groupsButton" 
         android:textSize="17sp" /> 
        <Button 
         android:layout_weight="0.33333333333" 
         android:layout_height="wrap_content" 
         android:text="@string/favouritesString" 
         android:layout_width="fill_parent" 
         android:id="@+id/button3" 
         android:textSize="17sp" /> 
       </LinearLayout> 
       <TextView 
        android:id="@+id/textView1" 
        android:text="@string/messageString" 
        android:layout_height="wrap_content" 
        android:textSize="17sp" 
        android:layout_width="fill_parent" /> 
       <EditText 
        android:layout_height="wrap_content" 
        android:id="@+id/editText1" 
        android:layout_width="fill_parent" 
        android:gravity="top|left" 
        android:minHeight="105dp" /> 
       <TextView 
        android:id="@+id/textView2" 
        android:text="@string/repetition" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:textSize="17sp" /> 
       <Spinner 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/spinner" /> 
       <LinearLayout 
        android:layout_height="wrap_content" 
        android:id="@+id/linearLayout3" 
        android:layout_width="fill_parent" > 
        <ImageView 
         android:src="@drawable/button_time" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/imageView1" 
         android:layout_weight="0.1" /> 
        <EditText 
         android:layout_height="wrap_content" 
         android:id="@+id/timeET" 
         android:inputType="number" 
         android:layout_width="wrap_content" 
         android:layout_weight="0.4" /> 
        <ImageView 
         android:src="@drawable/button_date" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/imageView2" 
         android:layout_weight="0.1" /> 
        <EditText 
         android:layout_height="wrap_content" 
         android:id="@+id/dateET" 
         android:inputType="number" 
         android:layout_width="wrap_content" 
         android:layout_weight="0.4" 
         android:layout_marginRight="3dp" /> 
       </LinearLayout> 
       <RelativeLayout 
        android:id="@+id/relativeLayout2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/linearLayout4" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentLeft="true" > 
         <Button 
          android:layout_weight="0.5" 
          android:layout_height="wrap_content" 
          android:text="@string/button_ok" 
          android:layout_width="fill_parent" 
          android:id="@+id/button4" 
          android:textSize="17sp" /> 
         <Button 
          android:layout_height="wrap_content" 
          android:layout_width="fill_parent" 
          android:id="@+id/button5" 
          android:layout_weight="0.5" 
          android:text="@string/button_cancel" 
          android:textSize="17sp" /> 
        </LinearLayout> 
       </RelativeLayout> 
      </LinearLayout> 
     </RelativeLayout> 
    </LinearLayout> 
</ScrollView> 
+0

button_ok和將Button4(那按鈕取消)看看不再在屏幕的BUTTOM和keyboeard開闢了沒有理由...... – Belgi

+0

你'

+0

發佈的代碼沒有打開鍵盤。確實,這些按鈕比屏幕上的其他任何東西都要低,但時間和日期之間的時間間隔EditText現在消失了,所以按鈕的高度更高。 – Belgi

1

這是一個比接受的答案更簡單修復。關鍵是<item name="android:windowSoftInputMode">adjustUnspecified</item>線。將它添加到您的styles.xml:

<style name="AppTheme" parent="@android:Theme.Holo.Light.DarkActionBar"> 
    <item name="android:alertDialogTheme">@style/iconPopUpDialogTheme</item> 
</style> 

<style name="DialogAppTheme" parent="AppTheme"> 
    <item name="android:dialogTheme">@style/iconPopUpDialogTheme</item> 
</style> 

<style name="PopUpDialogTheme"> 
    <item name="android:windowSoftInputMode">adjustUnspecified</item> 
</style>