【问题标题】:Programmatically scrolling an EditText以编程方式滚动 EditText
【发布时间】:2011-01-16 10:24:26
【问题描述】:

我正在编写一个简单的凯撒加密活动。屏幕上有两个 EditText,一个是明文,一个是加密的。这是加密的 EditText 的示例 - 明文类似。

<EditText 
    android:layout_below="@id/Caesar_Label_CryptText"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:id="@+id/Caesar_Text_CryptText" 
    android:hint="Enter crypted text"
    android:maxLines="2" 
    android:lines="2" 
    android:minLines="2"
    android:inputType="text|textMultiLine|textVisiblePassword"
    android:scrollbars="vertical" 
    android:gravity="top" />

现在,当输入明文时,我有一个 TextChangedListener 正在运行,它以编程方式加密并填充该加密编辑文本。到目前为止,一切顺利。

当输入的明文变得很长时,明文编辑文本会随着我的输入滚动,但密码编辑文本会停留在文本的顶部。我真的很希望 crypto-EditText 滚动,以便它始终显示其内容的底线。

如何做到这一点,最好是通过 TextWatcher 的 onTextChanged() 方法?

【问题讨论】:

  • 有趣的问题。看起来这应该是可能的,但我浏览了文档并找不到任何东西。

标签: android android-widget android-edittext


【解决方案1】:

好的,找到了。它是光标(在 EditText 和 TextViews 上称为选择)。

这就是我让它工作的方式:

ivClear    // assigned the EditText that has the input
ivCrypt    // assigned the target EditText, that I want to scroll
aText      // the input from ivClear, crypted

然后使用:

    ivCrypt.setText(aText);                               // assign the Text
    ivCrypt.setSelection(ivClear.getSelectionStart());    // scroll

呼,终于 :) 总是低估 Spannable 的力量 ;)

【讨论】:

    【解决方案2】:

    基类 android.view.View 有方法 getScrollX()、getScrollY() 和 scrollTo(),虽然我没有尝试过,但它们可能会有所帮助。

    http://developer.android.com/reference/android/view/View.html#scrollTo(int, int)

    【讨论】:

    • 我试过了。我记录了我的 cleartext-EditText 的 getScrollY()-value。输入文本时,此值会增加。然后我在crypto-EditText中滚动ToY(),开始时具有相同的值。但是第二个 EditText 不会滚动。我调试了scrollTo(),但是当我下次调用scrollTo() 时,View 中最初更改的mScrollY 变量被重置为0。我目前怀疑光标位置是罪魁祸首。对此进行调查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2010-11-13
    • 2014-05-22
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多