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>(); 
    } 
} 
相关问题