Android的EDITTEXT:在活动的onCreate获取LineCount() [英] Android Edittext: Get LineCount in an Activity's onCreate()
问题描述
我怎样才能做一个EDITTEXT的getLineCount()在活动的onCreate()方法,已经改变了EDITTEXT的文字,如下所示:
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();
}
由于该视图尚未拟定尚未getLineCount()始终返回0是有办法来解决这个问题呢?谢谢!
Because the view has not been drawn yet getLineCount() will always return 0. Is there a way to get around this problem? Thanks!
推荐答案
这绝对是一种痛苦。在我来说,我并不需要编辑,所以我用的TextView
但看到的EditText
派生自<$ C工作$ C>的TextView ,你应该能够使用同样的方法。我子类的TextView
和实施 onSizeChanged
来调用一个新的监听我称之为 OnSizeChangedListener
。在听众可以拨打 getLineCount()
与有效的结果。
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.
的的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);
}
}
}
这篇关于Android的EDITTEXT:在活动的onCreate获取LineCount()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!