2017-01-02 12 views
0

を生成し、私はこのレイアウトの例があります。アンドロイド:ScrollView内部の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用タッチのリスナーに追加することができます!フレームアウト "action_bar_layout"は、常に下に存在する必要があります – anthony

+0

私はあなたを取得していない、あなたは精巧にお祈りください... –

0

は、あなたが

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

属性

は、以下を追加することにより、クリック可能とフォーカス可能な親ビュー(あなたの活動の内容ビュー)を作成し、編集テキストのoutsidetouchにキーボードを非表示にすることができます

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) 
関連する問題