2015-01-08 1 views
0

LinearLayoutScrollView을 사용하는 동안 어떻게 소프트 키보드 입력을 숨길 수 있습니까?ScrollView를 사용하는 동안 소프트 키보드 입력을 숨기는 방법

(1)

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    return true; 
} 

(2)

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    ScrollView myScrollView = (ScrollView) findViewById(R.id.scrollview); //of course scrollview was id in layout then 
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    return true; 
} 

: 이러한 솔루션 중 어떤 것도 의도 된 결과를 생성 없지만

나는, 내 활동 클래스에 다음을 구현하기 위해 노력했다 (3)

# 2와 같지만,대신에 .


이 세 가지 해결책 중 어느 것도 저에게 효과적이지 않았습니다.

내가 알아챈 한 가지는 layout.xml 파일에서 ScrollView을 제거하면 모든 것이 의도 한대로 작동한다는 것입니다.

+0

이벤트가 호출되고 있습니까? 모든 것이 정상인 것처럼 보입니다 –

+0

@RobinDijkhof 예. @Override를 사용했습니다. public boolean onTouchEvent (MotionEvent 이벤트) { InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow (getCurrentFocus(). getWindowToken(), 0); return true; }'내 이전 활동에서 작동하고 모든 것이 잘 작동합니다.제 의견으로는 ScrollView 구성 요소에 문제가 있습니다 (그러나 정확히 무엇을 말합니까?) 레이아웃 xml 파일 (레이아웃/레이아웃 포트)에 주석 ScrollView 구성 요소를 넣었을 때 모든 것이 정상이었습니다. – Tom

답변

0

아마도 ScrollView가 touch 이벤트를 소비하고있을 것입니다. 어쩌면 스크롤 뷰의 onFocusChangeListener를 연결하고 거기에서 키보드를 숨길 수 있습니까?

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

xml의 스크롤보기에 일부 속성을 추가해야 할 수도 있습니다.

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

메소드가 호출되었는지 확인하기 위해 onCreate 메소드를 코드에 넣고 에 log.w를 추가했습니다. 디버거에서 아무 것도 보지 못해서 화면을 건드린 후 메서드가 호출되지 않았습니다. – Tom

0
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_id_root); 
    setHideKeyboard(this, layout); 
} 

public void setHideKeyboard(final Context context, View view) { 
    try { 
     //Set up touch listener for non-text box views to hide keyboard. 
     if (!(view instanceof EditText || view instanceof ScrollView)) { 

      view.setOnTouchListener(new View.OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        InputMethodManager in = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
        in.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
        return false; 
       } 

      }); 
     } 

     //If a layout container, iterate over children and seed recursion. 
     if (view instanceof ViewGroup) { 

      for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

       View innerView = ((ViewGroup) view).getChildAt(i); 

       setHideKeyboard(context, innerView); 
      } 
     } 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 
0

첫째, EditText를 확장하고 키보드에게 글고 인스턴스가

public class MyEditText extends AppCompatEditText { 

public MyEditText(Context context) { 
    super(context); 
    setupEditText(); 
} 

public MyEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setupEditText(); 
} 

public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    setupEditText(); 
} 

public void setupEditText() { 
    // Any time edit text instances lose their focus, dismiss the keyboard! 
    setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus && !(findFocus() instanceof MyEditText)) { 
       hideKeyboard(v); 
      } else { 
       showKeyboard(v); 
      } 
     } 
    }); 
} 

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

public void showKeyboard(View view) { 
    InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.showSoftInput(view, 0); 
} 
} 

그런 다음, 아이에 android:clickable="true"android:focusableInTouchMode="true"을 설정

그들의 초점을 잃을 때마다 해고하는 데 도움이 코드의 조각을 추가 ScrollView의 레이아웃!

ScrollView 자체가 아닌 ScrollView의 하위 레이아웃이어야합니다.

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

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

    <MyEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    </MyEditText> 

</LinearLayout> 

</ScrollView> 

그래야합니다!

scrollView.setOnTouchListener(new View.OnTouchListener() 
{ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     if (event != null && event.getAction() == MotionEvent.ACTION_MOVE) 
     { 
      InputMethodManager imm = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)); 
      boolean isKeyboardUp = imm.isAcceptingText(); 

      if (isKeyboardUp) 
      { 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
     return false; 
    } 
}); 
: 내 경우
1

, 내가있는 ScrollView 안에 너무 많은 글고 필드를 구성하고 드디어 코드 아래 사용하여 해결할 수 너무 많은 옵션을 시도했지만 양식을 스크롤에 키보드를 숨길 동적 양식을 생성하고