2014-11-17 111 views
0

所有,我hava滚动包含editText。scrollView.requestFocus()返回true,但它不起作用

一旦我点击editText,它将获得焦点并显示一个SoftInput。

在我看来,如果我单击空格,softinput hide,editText会失去焦点,并且scrollview会获得焦点。

所以我这样的代码:

scrollView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      //call twice, once down, once up 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       if (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null) { 
        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
        scrollView.requestFocus(); 
        activity.getCurrentFocus(); 
       } 
      } 
      return false; 
     } 
    }); 



ScrollView 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:id="@+id/scrollView" 
     android:layout_below="@+id/topBar" 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

但毕竟还是EDITTEXT重点(activity.getCurrentFocus()== EDITTEXT),有何意见?

+0

以编程方式从编辑文本中删除焦点。 –

+0

@MurtazaHussain我使用'activity.getCurrentFocus()。clearFocus();',仍然没有发生 – Ninja

回答

0

使用LinearLayout而不是ScrollView。或者用下面的LinearLayout包装EditText。

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollView" 
    android:layout_gravity="center_horizontal" > 

    <LinearLayout 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/linearLayout" 
     android:layout_gravity="center_horizontal" > 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:ems="10" 
      android:id="@+id/editText" /> 
    </LinearLayout> 
</ScrollView> 
+0

你能指定什么是你的问题?你的代码是如何运作的以及你期望的是什么? – rhgb

相关问题