3

我的應用程序有一個總體佈局,包括一個水平滾動視圖。 我正在做的是將32個片段添加到滾動視圖(這將擴展到一個具有兩個按鈕和一個editText的垂直線性佈局,它們將成爲均衡器的控件)。Android - Horizo​​ntalScrollView在鍵盤打開時向右滾動

問題是,當我嘗試編輯其中一個editText的時候,scrollview會一直向右滾動 - 所以您無法看到您輸入的內容,並且必須一直向後滾動到該條目查看它。

這裏是主要的xml佈局。這將屏幕分成4個象限,與水平滾動視圖右下角會:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    tools:context=".MainActivity" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:padding="4dp" 
     android:orientation="vertical" 
     android:layout_weight="2" 
     > 
     <TextView android:text="Hello World!" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="4" 
     android:padding="4dp" 
     android:orientation="vertical" 
     android:background="#FF0000" 
     > 
     <TextView android:text="Hello World!"  android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 


     <HorizontalScrollView 
      android:id="@+id/hsv_eqControls" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dp" 
      android:background="#FFFFFF" 
      android:descendantFocusability="beforeDescendants" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      > 

      <LinearLayout 
       android:id="@+id/layout_eqControls" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal"> 

      </LinearLayout> 

     </HorizontalScrollView> 
    </LinearLayout> 

</LinearLayout> 

而這就是我加入到了滾動

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="3dp" 
    android:orientation="vertical"> 
    <Button 
     android:id="@+id/btn_eqUp" 
     android:layout_width="60dp" 
     android:layout_height="60dp" /> 
    <EditText 
     android:id="@+id/etext_eq" 
     android:layout_width="60dp" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal|numberSigned" 
     android:textAlignment="center" 
     android:layout_gravity="center_horizontal" 
     android:background="#EEEEEE" 
     android:hint="32dB"/> 
    <Button 
     android:id="@+id/btn_eqDown" 
     android:layout_width="60dp" 
     android:layout_height="60dp" /> 
    <TextView 
     android:id="@+id/vtext_eqLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAlignment="center" 
     android:layout_gravity="center" 
     android:text="1"/> 
</LinearLayout> 

這是我的代碼「M運行,填補了滾動型:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // View eqControls = findViewById(R.id.hsv_eqControls); 

     FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     for(int i = 1; i < 33; i++) 
     { 
      ft.add(R.id.layout_eqControls, new FragmentEQ(), "ch"+i); 
     } 

     ft.commit(); 
     getFragmentManager().executePendingTransactions(); 
     for(int i = 1; i < 33; i++) 
     { 
      FragmentEQ feq = (FragmentEQ) getFragmentManager().findFragmentByTag("ch"+i); 
      if(feq != null) 
       feq.setChannel(i); 
     } 

    } 
} 

這是我的片斷代碼:

public class FragmentEQ extends Fragment { 
    protected int _channel = 0; 
    protected ViewGroup _parent; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.activity_fragment_eq, container, false); 
     TextView label = (TextView) view.findViewById(R.id.vtext_eqLabel); 

     label.setText(Integer.toString(_channel)); 
     return view; 
    } 

    public int getChannel() { return _channel; } 

    public void setChannel(int i) 
    { 
     _channel = i; 
    } 
} 

我試過創建一個自定義Horizo​​ntalScrollView,並重寫scrollTo()和onRequestFocusInDescendants()作爲空白的方法,什麼都不做,但這不會改變。

public class EQScrollView extends HorizontalScrollView { 

    public EQScrollView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     setSmoothScrollingEnabled(false); 
    } 

    @Override 
    public void scrollTo(int x, int y) 
    { 

    } 

    @Override 
    public boolean onRequestFocusInDescendants(int x, Rect y) 
    { 

     return false; 
    } 

} 
+0

[檢查這一點。 ..](http://stackoverflow.com/questions/3295672/android-soft-keyboard-covers-edittext-field) – Biplab

回答

1

我剛剛遇到了這個問題,對我滾動到結束的時候android:inputType未在EditText設置纔會發生。如果你刪除這個問題,那麼問題就會消失。

僅供參考即使使用.setInputType(InputType.TYPE_CLASS_NUMBER);也會產生此錯誤。

無可否認,這不是一個解決辦法,但值得注意的原因。

+0

指出我在正確的方向 - 謝謝 –

0

使用 「設置原始的inputType」:

setRawInputType(InputType.TYPE_CLASS_NUMBER); 

解決這個問題,我在第一。意識到一些數字鍵盤不提供十進制輸入我結束了使用標準文本輸入的EditText,延長Horizo​​ntalScrollView和壓倒一切「onRequestFocusInDescendants」後,「requestChildRectangleOnScreen」和「getFocusables」像這樣:

public class HorizontalScrollViewEx extends HorizontalScrollView { 
    public HorizontalScrollViewEx(Context context) { 
     super(context); 
    } 

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

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

    public HorizontalScrollViewEx(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 
     return true; 
    } 

    @Override 
    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { 
     return true; 
    } 

    @Override 
    public ArrayList<View> getFocusables(int direction) { 
     return new ArrayList<View>(); 
    } 
} 
相關問題