Android Edittext:在 Activity 的 onCreate() 中获取 LineCount

发布于 2024-12-14 15:53:12 字数 311 浏览 2 评论 0原文

如何在活动的 onCreate() 方法中执行 Edittext 的 getLineCount() 操作,更改 Edittext 的文本,如下所示:

    @override
    public void onCreate(Bundle savedInstanceState){
        myEditText.setText("EXAMPLE");
        myEditText.getLineCount();
    }

因为视图尚未绘制, getLineCount() 将始终返回 0。有没有办法解决这个问题?谢谢!

How can I do getLineCount() of an Edittext in the onCreate() method of an activity, having changed the Edittext's text, like the following:

    @override
    public void onCreate(Bundle savedInstanceState){
        myEditText.setText("EXAMPLE");
        myEditText.getLineCount();
    }

Because the view has not been drawn yet getLineCount() will always return 0. Is there a way to get around this problem? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-12-21 15:53:12

呃,到处都是 UI 的问题。

您可以使用处理程序。您将发布一个 Runnable 来获取行数并继续处理。

Ugh, it's a problem with UI everywhere.

You can use a Handler. You'll post a Runnable that will get the line count and continue the processing.

〆一缕阳光ご 2024-12-21 15:53:12

这绝对是一种痛苦。就我而言,我不需要编辑,所以我使用 TextView 但看到 EditText 派生自 TextView 你应该能够使用同样的方法。我对 TextView 进行了子类化并实现了 onSizeChanged 来调用一个新的侦听器,我将其称为 OnSizeChangedListener。在侦听器中,您可以调用 getLineCount() 并获取有效结果。

TextView

/** Size change listening TextView. */
public class SizeChangeNotifyingTextView extends TextView {
    /** Listener. */
    private OnSizeChangeListener m_listener;

    /**
     * Creates a new Layout-notifying TextView.
     * @param context   Context.
     * @param attrs     Attributes.
     */
    public SizeChangeNotifyingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Adds a size change listener.
     * @param listener  Listener.
     */
    public void setOnSizeChangedListener(OnSizeChangeListener listener) {
        m_listener = listener;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (m_listener != null) {
            m_listener.onSizeChanged(w, h, oldw, oldh);
        }
    }
}

This is definitely a pain. In my case I didn't need editing, so I worked with TextView but seeing as EditText derives from TextView you should be able to use the same approach. I subclassed TextView and implemented onSizeChanged to call a new listener which I called OnSizeChangedListener. In the listener you can call getLineCount() with valid results.

The TextView:

/** Size change listening TextView. */
public class SizeChangeNotifyingTextView extends TextView {
    /** Listener. */
    private OnSizeChangeListener m_listener;

    /**
     * Creates a new Layout-notifying TextView.
     * @param context   Context.
     * @param attrs     Attributes.
     */
    public SizeChangeNotifyingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Adds a size change listener.
     * @param listener  Listener.
     */
    public void setOnSizeChangedListener(OnSizeChangeListener listener) {
        m_listener = listener;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (m_listener != null) {
            m_listener.onSizeChanged(w, h, oldw, oldh);
        }
    }
}
ぃ弥猫深巷。 2024-12-21 15:53:12

非常感谢最后的回答,对我很有帮助!

作为贡献,我想添加将注册到 SizeChangeNotifyingTextView 钩子的侦听器接口的代码(可以添加到 SizeChangeNotifyingTextView 类中):

public interface OnSizeChangeListener {
    public void onSizeChanged(int w, int h, int oldw, int oldh);
}

最后,要注册侦听器,您可以这样做:

tv.setOnSizeChangedListener(new SizeChangeNotifyingTextView.OnSizeChangeListener() {
     @Override
     public void onSizeChanged(int w, int h, int oldw, int oldh) {
       ...
     }
});

Thanks a lot for the last answer, it was very helpful to me!

As a contribution, I would like to add the code of the listener interface that will be registered to the SizeChangeNotifyingTextView's hook (which can be added inside the SizeChangeNotifyingTextView class):

public interface OnSizeChangeListener {
    public void onSizeChanged(int w, int h, int oldw, int oldh);
}

Finally, to register a listener, you can do it this way:

tv.setOnSizeChangedListener(new SizeChangeNotifyingTextView.OnSizeChangeListener() {
     @Override
     public void onSizeChanged(int w, int h, int oldw, int oldh) {
       ...
     }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文