滚动视图自动滚动输入软键盘的输入键

问题描述:

我有一个简单的布局,它在xml里面有一个嵌套元素ScrollView,它基本上是一个获取多个EditText和其他对象的用户输入的表单。 我EditText一个有属性滚动视图自动滚动输入软键盘的输入键

android:lines="2" 

用户地址栏,它必须有2行。所以当用户输入到EditText时,在软输入键盘上显示回车键,并重复按此回车键主Scrollview自动滚动到底部,每次点击输入和EditText失去其焦点和布局自动滚动到底部它。如何阻止这种情况发生。

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/rel_main_footer" 
    android:layout_below="@+id/enquiry_header" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/tv_wit_header" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="@string/wit_head" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#7f0100" /> 
     <!-- Owner Details --> 

     <LinearLayout 
      android:id="@+id/li1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tv_wit_header" 
      android:orientation="horizontal" 
      android:padding="8dp" > 

      <TextView 
       android:id="@+id/tv_wit_owner" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:text="@string/wit_name" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <EditText 
       android:id="@+id/et_wit_owner" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:background="#00000000" 
       android:hint="@string/hint_set_owner" 
       android:inputType="text" 
       android:singleLine="true" 
       android:textColor="#7f0000" 
       android:textColorHint="#a65b5a" /> 
     </LinearLayout> 

     <View 
      android:id="@+id/v1" 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:layout_below="@+id/li1" 
      android:background="#A4A4A4" /> 

     <LinearLayout 
      android:id="@+id/li2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/v1" 
      android:orientation="horizontal" 
      android:padding="8dp" > 

      <TextView 
       android:id="@+id/tv_wit_address" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:text="@string/ud_address" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <EditText 
       android:id="@+id/et_wit_address" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:background="#00000000" 
       android:gravity="top|left" 
       android:hint="@string/hint_set_address" 
       android:lines="2" 
       android:singleLine="true" 
       android:scrollbars="vertical" 
       android:textColor="#7f0000" 
       android:textColorHint="#a65b5a" /> 
     </LinearLayout> 

     <View 
      android:id="@+id/v2" 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:layout_below="@+id/li2" 
      android:background="#A4A4A4" /> 

     <LinearLayout 
      android:id="@+id/li3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/v2" 
      android:orientation="horizontal" 
      android:padding="8dp" > 

      <TextView 
       android:id="@+id/tv_wit_number" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:text="@string/wit_number" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <EditText 
       android:id="@+id/et_wit_number" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:background="#00000000" 
       android:hint="@string/hint_wit_number" 
       android:inputType="phone" 
       android:singleLine="true" 
       android:textColor="#7f0000" 
       android:textColorHint="#a65b5a" /> 
     </LinearLayout> 

     <View 
      android:id="@+id/v3" 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:layout_below="@+id/li3" 
      android:background="#A4A4A4" /> 

     <LinearLayout 
      android:id="@+id/li4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/v3" 
      android:orientation="vertical" 
      android:padding="8dp" > 

      <TextView 
       android:id="@+id/tv_wit_notes" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/wit_notes" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <EditText 
       android:id="@+id/et_wit_notes" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#00000000" 
       android:gravity="top|left" 
       android:hint="@string/hint_wit_notes" 
       android:inputType="textMultiLine" 
       android:lines="5" 
       android:maxLines="5" 
       android:textColor="#7f0000" 
       android:textColorHint="#a65b5a" /> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
+0

尝试在你的活动声明中加入这一行的AndroidManifest.xml - 机器人:windowSoftInputMode =“stateHidden | adjustResize” –

+0

让我用这个 –

+0

不,这是行不通的 –

它不是你的问题,100%正确的解决方案,但它可能是解决这个问题

et_wit_address.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence s, int start, 
       int before, int count) { 
      scrollview.scrollTo(0, et_wit_number.getTop()); 
         } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, 
       int count, int after) { 
      scrollview.scrollTo(0, et_wit_number.getTop()); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      scrollview.scrollTo(0, et_wit_number.getTop()); 
     } 
    }); 

可以使用android:imeOptions处理键盘上额外的按钮。

ActionNext:

操作键进行“下一步”操作,从而将用户的下一个字段将接受的文本。

ActionDone

操作键执行“完成”的操作,通常意味着有什么更多的输入和输入法将被关闭。

例子:

<RelativeLayout 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" 
    tools:context=".MainActivity" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="32dp" 
     android:layout_marginTop="16dp" 
     android:imeOptions="actionNext" 
     android:singleLine="true" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="24dp" 
     android:imeOptions="actionDone" 
     android:singleLine="true" 
     android:ems="10" /> 

</RelativeLayout>