2014-11-17 179 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

相關問題