2014-01-29 31 views
0

我有以下佈局:機器人屏幕我向下滾動上的每個輸入到EDITTEXT插入

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue_bg" > 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_horizontal" 
       android:orientation="vertical" > 


.... 
       <View 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="0.3" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="20dp" 
       android:src="@drawable/signup_illu_verificationcode" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="20dp" 
       android:layout_weight="0.0" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:background="@android:color/white" 
       android:padding="5dp" 
       android:paddingLeft="25dp" 
       android:paddingRight="25dp" > 

       <LinearLayout 
        android:id="@+id/inputBox" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:layout_gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:background="@drawable/input_box_idle" 
        android:gravity="center" 
        android:orientation="horizontal" > 

        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <EditText 
          android:id="@+id/verificationCodeEditText" 
          style="@style/textOnBg" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:background="@android:color/transparent" 
          android:hint="- - - - -" 
          android:inputType="number" 
          android:textColor="#516063" 
          android:textColorHint="#b4c8cf" 
          android:textSize="25dp" > 
         </EditText> 
        </LinearLayout> 
       </LinearLayout> 
      </LinearLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="0.7" /> 

      ... 

     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

和下面的代碼

其中假設檢測softkeboard開口和滾動視圖有點向下(一次)

public class PhoneVerifyYourNumbersActivity extends Activity { 

     private String mDisplayOptions[] = new String[3]; 
     private LinearLayout mInputBox; 
     private LinearLayout mContinueButton; 
     private TextView mVerifyByPhoneCallText; 

     private ScrollView mScrollView; 

     private EditText mVerificationCodeEditText; 
     private String mHash = null; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.phone_verify_your_numbers); 

      mHash = getIntent().getStringExtra("Hash"); 

      initMembers(); 
      setOnClickListeners(); 
      initFieldsTexts(); 
      setKeyboardVisibilityListener(); 

     } 

.. 

     private void setKeyboardVisibilityListener() { 
      final View root = findViewById(R.id.rootLayout); 

      root.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() { 

         @Override 
         public void onGlobalLayout() { 
          // TODO Auto-generated method stub 

          int heightDiff = root.getRootView().getHeight() 
            - root.getHeight(); 
          if (heightDiff > 100) { // more than 100 pixels is 
                // probably a keyboard 
           // keyboard is shown 
           mInputBox 
             .setBackgroundResource(R.drawable.input_box_active); 
           //mScrollView.scrollTo(0, mScrollView.getBottom()); 
           mScrollView.scrollBy(0, +20); 
          } else { 
           // keyboard is not shown 
           mInputBox 
             .setBackgroundResource(R.drawable.input_box_idle); 
          } 
         } 
        }); 
     } 
} 

} 

我的清單:

<activity 
    android:name=".phone.PhoneVerifyYourNumbersActivity" 
    android:theme="@android:style/Theme.NoTitleBar" 
    android:windowSoftInputMode="stateHidden|adjustResize" /> 

一個奇怪的行爲是,每當用戶向EditText插入輸入時,滾動會以1-2dps的速度向下移動。什麼會導致這種情況?

我想這mScrollView.scrollBy(0, +20);發生多次

但我怎麼可以讓它發生一次?

(除了添加布爾標誌isMovedAlready以外的任何聰明的想法)?

回答

0

mScrollView.scrollBy()滾動px(像素)不是dp。你可能首先要計算dp到px。
但我想你想要不同的東西。是這樣的:

View v = findViewById(R.id.verificationCodeEditText); 
mScrollView.scrollTo(0, v.getBottom()+10);//or similar 
+0

但這個'onGlobalLayout'只調用一次後的鍵盤呢?不是每一個輸入插入editText? –

+1

我不確定這一點。特別是不適用於所有的android版本。但它也可以被其他原因調用。 '當全局佈局狀態或視圖樹中的視圖可見性發生變化時要調用的回調方法,以便幾乎每個視圖/視圖佈局更改操作都可以導致呼叫。 –

0
// try to add one properties on AndroidManifest.xml Activity declaration like below. 
**AndroidManifest.xml** 
<activity 
    android:name="PhoneVerifyYourNumbersActivity" 
    android:windowSoftInputMode="stateHidden|adjustPan"> 
</activity> 
+0

我已添加目前的清單。不是這個'onGlobalLayout'在鍵盤輸出後只調用一次?不是每一個輸入插入editText? –

相關問題