Android版的EditText文本输入导致滚动 [英] Android EditText text entry causing scroll

查看:238
本文介绍了Android版的EditText文本输入导致滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个类似的问题我刚才发布的 - 但不同的

我有一个包含3个页面一Horizo​​ntalScrollView。当我的应用程序启动时,我告诉滚动视图滚动一个屏幕的宽度,使中间的页面总是首先显示。见下图:

  + ------ + ------ + ------ +
+ ET + +
+ ------ + +
++++
++++
+ ------ + ------ + ------ +
       ^^

以上ET重新presents一个EditText视图。为了从自动滚动到最左边的页面停止滚动型我,我叫setFocusable(假)上的EditText。这让我的应用程序留在中央页面上。

当我滚动到最左边的页面,我打电话setFocusableInTouchMode(真),以允许在文本的EditText入境。

现在来到陌生的地方:

1)当我在我的平板电脑输入文字的 ,滚动视图滚动到最右边的页面。我的平板电脑有一个物理键盘。

2)我从平板电脑中删除物理键盘。当我在我的平板电脑输入文字的 ,滚动视图滚动到最右侧的页面就显示virual键盘。

3)当我我的电话上输入文本的,滚动视图仍然是最左边的页面上,因为它应该。我的手机显示,当我在的EditText敲击虚拟键盘。

为什么我的平板电脑这样做时,我的手机是不是?我的平板电脑比我的手机老,显然Android的旧版本,但是什么导致这种行为,我怎么阻止它?

我将提供样品code,但有很多分解出。我不是在此位code的使用XML布局无论是。最右边的页面中的按钮 - 唯一的EditText是最左边的页面上

更新:如果我删除了最右边的页面,它仍然跳转到第二个页面,而不是最左边。我似乎不具有第二页上的任何其他聚焦的观点无论是。

下面是一个简单的MainActivity.java这说明我的问题。请注意,所以我怀疑它只会在特定的Andr​​oid版本中发生不我的手机上出现。我的平板电脑运行的是Android 4.2.1及华硕变压器垫TF300TG。

 包com.example.testscroll;进口android.app.Activity;
进口android.graphics.Color;
进口android.graphics.Point;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.Gravity;
进口android.widget.EditText;
进口android.widget.Horizo​​ntalScrollView;
进口android.widget.LinearLayout;
进口android.widget.ListView;公共类MainActivity扩展活动
{
  公共Horizo​​ntalScrollView testScrollView;
  公众诠释屏幕宽度,screenHeight;  的LinearLayout mLeftBaseLinearLayout;
  的EditText mEditText;
  ListView的mListView;  TextWatcher mTextWatcher =新TextWatcher()
  {
    @覆盖
    公共无效afterTextChanged(编辑S)
    {
      串sSimple = s.toString();
      System.out.printf(现在文本[+ sSimple +] \\ n);
    }
    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){}    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
  };
  @覆盖
  保护无效的onCreate(捆绑savedInstanceState)
  {
    super.onCreate(savedInstanceState);    点大小=新点();    。getWindowManager()getDefaultDisplay()的getSize(大小)。    屏幕宽度= size.x;
    screenHeight = size.y;    的LinearLayout布局=新的LinearLayout(本);
    layout.setLayoutParams(新LinearLayout.LayoutParams(3 *屏幕宽度,LinearLayout.LayoutParams.MATCH_PARENT));
    layout.setOrientation(LinearLayout.HORIZONTAL);    // --------左页--------
    mLeftBaseLinearLayout =新的LinearLayout(本);
    mLeftBaseLinearLayout.setLayoutParams(新LinearLayout.LayoutParams(屏幕宽度,screenHeight));
    mLeftBaseLinearLayout.setOrientation(LinearLayout.VERTICAL);
    mLeftBaseLinearLayout.setBackgroundColor(Color.TRANSPARENT);    mEditText =新的EditText(本);    mEditText.setWidth(屏幕宽度);
    mEditText.setHeight(20);
    mEditText.setBackgroundColor(Color.DKGRAY);
    mEditText.setGravity(Gravity.CENTER);
    mEditText.setText();
    mEditText.setTextColor(Color.YELLOW);
    mEditText.setTextSize(30);
    mEditText.setSingleLine();
    mEditText.setLayoutParams(新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));    //mEditText.setFocusable(假);
    mEditText.setFocusable(真);
    mEditText.addTextChangedListener(mTextWatcher);    mListView =新的ListView(这一点);
    mListView.setBackgroundColor(Color.WHITE);
    mListView.setLayoutParams(新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1));    //mLeftBaseLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    mLeftBaseLinearLayout.addView(mEditText);
    mLeftBaseLinearLayout.addView(mListView);
    // -------    layout.addView(mLeftBaseLinearLayout);    的LinearLayout linLayout =新的LinearLayout(本);
    linLayout.setBackgroundColor(Color.GREEN);
    linLayout.setLayoutParams(新LinearLayout.LayoutParams(1280,LinearLayout.LayoutParams.MATCH_PARENT));
    layout.addView(linLayout);    testScrollView =新Horizo​​ntalScrollView(本);
    testScrollView.setLayoutParams(新LinearLayout.LayoutParams(3 *屏幕宽度,LinearLayout.LayoutParams.MATCH_PARENT));
    testScrollView.addView(布局);    的setContentView(testScrollView);    testScrollView.post(新的Runnable()
    {
      公共无效的run()
      {
        testScrollView.scrollTo(屏幕宽度,0);
      }
    });
  }
}


解决方案

我发现我的解决方案 - 覆盖scrollTo()和什么都不做。

This is a similar problem to what I posted earlier - but different.

I have a HorizontalScrollView which contains 3 "pages". When my app starts up, I tell the scrollview to scroll one screen width so that the middle page is always displayed first. See below:

+------+------+------+
+  ET  +      +      +
+------+      +      +
+      +      +      +
+      +      +      +
+------+------+------+
       ^      ^

ET above represents an EditText view. In order to stop my scrollview from automatically scrolling to the leftmost page, I call setFocusable(false) on the EditText. This allows my app to stay on the central page.

When I scroll to the leftmost page, I call setFocusableInTouchMode(true) to allow entry of text in the EditText.

Now come the strange parts:

1) When I enter text on my tablet, the scrollview scrolls to the rightmost page. My tablet has a physical keyboard.

2) I remove the physical keyboard from my tablet. When I enter text on my tablet, the scrollview scrolls to the rightmost page as soon as the virual keyboard is displayed.

3) When I enter text on my phone, the scrollview remains on the leftmost page as it should. My phone displays the virtual keyboard when I tap on the EditText.

Why is my tablet doing this when my phone isn't? My tablet is older than my phone and obviously has an older version of Android but what's causing this behaviour and how do I stop it?

I would provide sample code but there's a lot to factor out. I'm not using an XML layout in this bit of code either. The rightmost page consists of buttons - the only EditText is on the leftmost page.

Update: If I remove the rightmost page, it still jumps to the second page instead of the leftmost. I don't seem to have any additional focusable views on that second page either.

Here's a simplified MainActivity.java which illustrates my problem. Note that it doesn't happen on my phone so I suspect it will only happen on certain Android versions. My tablet is running Android 4.2.1 and its an Asus Transformer Pad TF300TG.

package com.example.testscroll;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity
{
  public HorizontalScrollView testScrollView;
  public int screenWidth, screenHeight;

  LinearLayout mLeftBaseLinearLayout;
  EditText     mEditText;
  ListView     mListView;

  TextWatcher mTextWatcher = new TextWatcher() 
  {
    @Override
    public void afterTextChanged( Editable s ) 
    {
      String sSimple = s.toString();
      System.out.printf(  "Text now[" + sSimple + "]\n" );
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count){}     
  };


  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );

    Point size = new Point();

    getWindowManager().getDefaultDisplay().getSize( size );

    screenWidth  = size.x;
    screenHeight = size.y;

    LinearLayout layout = new LinearLayout( this );
    layout.setLayoutParams( new LinearLayout.LayoutParams( 3 * screenWidth, LinearLayout.LayoutParams.MATCH_PARENT ) );
    layout.setOrientation( LinearLayout.HORIZONTAL );

    //-------- LEFT PAGE --------
    mLeftBaseLinearLayout = new LinearLayout( this );
    mLeftBaseLinearLayout.setLayoutParams( new LinearLayout.LayoutParams( screenWidth, screenHeight ) );
    mLeftBaseLinearLayout.setOrientation( LinearLayout.VERTICAL );
    mLeftBaseLinearLayout.setBackgroundColor( Color.TRANSPARENT );

    mEditText = new EditText( this );

    mEditText.setWidth( screenWidth );
    mEditText.setHeight( 20 );
    mEditText.setBackgroundColor( Color.DKGRAY );
    mEditText.setGravity( Gravity.CENTER );
    mEditText.setText( "" );
    mEditText.setTextColor( Color.YELLOW );
    mEditText.setTextSize( 30 );
    mEditText.setSingleLine();
    mEditText.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ) );

    //mEditText.setFocusable( false );
    mEditText.setFocusable( true );
    mEditText.addTextChangedListener( mTextWatcher );

    mListView = new ListView( this );
    mListView.setBackgroundColor( Color.WHITE );
    mListView.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1 ) );

    //mLeftBaseLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    mLeftBaseLinearLayout.addView( mEditText );    
    mLeftBaseLinearLayout.addView( mListView );
    //-------    

    layout.addView( mLeftBaseLinearLayout );

    LinearLayout linLayout   = new LinearLayout( this );
    linLayout.setBackgroundColor( Color.GREEN );    
    linLayout.setLayoutParams( new LinearLayout.LayoutParams( 1280, LinearLayout.LayoutParams.MATCH_PARENT ) );
    layout.addView( linLayout );

    testScrollView     = new HorizontalScrollView( this );
    testScrollView.setLayoutParams( new LinearLayout.LayoutParams( 3 * screenWidth, LinearLayout.LayoutParams.MATCH_PARENT ) );
    testScrollView.addView( layout );

    setContentView( testScrollView );

    testScrollView.post( new Runnable() 
    { 
      public void run() 
      { 
        testScrollView.scrollTo( screenWidth, 0 );
      } 
    });    
  }
}

解决方案

I found my solution - override scrollTo() and do absolutely nothing.

这篇关于Android版的EditText文本输入导致滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆