2017-01-02 64 views
0

我有這樣的佈局實例:安卓的EditText內滾動型:隱藏鍵盤產生奇怪的滾動

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="56dp" 
      android:background="#FFF880" 
      android:gravity="center_vertical" 
      android:orientation="horizontal"> 

     </FrameLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ScrollView 
        android:id="@+id/scroll" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/action_bar_layout"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <TextView 
          android:id="@+id/emotion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textColor="@android:color/holo_blue_bright" /> 

         <EditText 
          android:id="@+id/edit" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:background="@android:color/transparent" 
          android:inputType="textMultiLine|textCapSentences" 
          android:singleLine="false" 
          android:text="On sait depuis longtemps " 
          android:textColor="@android:color/black" /> 

         <FrameLayout 
          android:id="@+id/photo" 
          android:layout_width="match_parent" 
          android:layout_height="350dp" 
          android:background="#EE00FF"> 

         </FrameLayout> 

        </LinearLayout> 

       </ScrollView> 

       <FrameLayout 
        android:id="@+id/action_bar_layout" 
        android:layout_width="match_parent" 
        android:layout_height="56dp" 
        android:layout_alignParentBottom="true" 
        android:background="#990000" 
        android:gravity="center_vertical" 
        android:orientation="horizontal"> 

       </FrameLayout> 

      </RelativeLayout> 

     </RelativeLayout> 

    </LinearLayout> 

</RelativeLayout> 

而且我的樣本活動:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edittext); 
     final ScrollView scrollView = (ScrollView)findViewById(R.id.scroll); 
     final EditText edittext = (EditText)findViewById(R.id.edit); 

     // Setup scrollview 
     scrollView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       final int action = event.getAction(); 
       switch (action) { 
        case MotionEvent.ACTION_MOVE: 
         // Hide keyboard 
         UIUtils.hideKeyboard(edittext, EditTextActivity.this); 
         break; 
       } 
       return false; 
      } 
     }); 

public static void hideKeyboard(View view, Activity activity) { 
     if (view != null) { 
      view.clearFocus(); 
      if (activity != null) { 
       activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
       InputMethodManager imm = (InputMethodManager) activity. 
         getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
      } 
     } 
    } 

所以我想Facebook等新的職位相同的行爲:當你想滾動鍵盤時自動消失。

但在我的情況下,當鍵盤被隱藏時想要滾動時會出現奇怪的行爲。

謝謝你們的幫助!

回答

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/action_bar_layout" 
    android:fillViewport="true"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="56dp" 
       android:background="#FFF880" 
       android:gravity="center_vertical" 
       android:orientation="horizontal"> 

      </FrameLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <TextView 
          android:id="@+id/emotion" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textColor="@android:color/holo_blue_bright" /> 

         <EditText 
          android:id="@+id/edit" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:background="@android:color/transparent" 
          android:inputType="textMultiLine|textCapSentences" 
          android:singleLine="false" 
          android:text="On sait depuis longtemps " 
          android:textColor="@android:color/black" /> 

         <FrameLayout 
          android:id="@+id/photo" 
          android:layout_width="match_parent" 
          android:layout_height="350dp" 
          android:background="#EE00FF"> 

         </FrameLayout> 

        </LinearLayout> 


        <FrameLayout 
         android:id="@+id/action_bar_layout" 
         android:layout_width="match_parent" 
         android:layout_height="56dp" 
         android:layout_alignParentBottom="true" 
         android:background="#990000" 
         android:gravity="center_vertical" 
         android:orientation="horizontal"> 

        </FrameLayout> 

       </RelativeLayout> 

      </RelativeLayout> 

     </LinearLayout> 

    </RelativeLayout> 
</ScrollView> 

試試這個希望它能解決你的問題。

+0

沒有Risbhabh觸摸聽者補充! framelyout「action_bar_layout」必須始終存在於底部 – anthony

+0

我沒有得到你,請你詳細說明...... –

0

相反的滾動隱藏的鍵盤,你可以在outsidetouch編輯文本的隱藏鍵盤

通過添加下面讓父視圖(你的活動內容視圖),可以點擊的,可聚焦的屬性

android:clickable="true" 
    android:focusableInTouchMode="true" 

實現一個hideKeyboard()方法

public void hideKeyboard(View view) { 
     InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 

,並設置您的EditText的onFocusChangeListener。

edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
       hideKeyboard(v); 
      } 
     } 
    }); 

,或者如果你想隱藏滾動,那麼你可以爲你的主視圖,而不是滾動視圖

findViewById(R.id.mainLayout).setOnTouchListener(this)