如何检查TextView是否可滚动

时间:2014-11-17 17:10:08

标签: android textview

如果它有超过5行,我可以滚动TextView。现在我想动态地在我的java代码中检测相同的TextView是否可滚动或者可以滚动,以便我可以更改它下面的视图的可见性。

我试过了:

textView.setText(description);
textView.setMovementMethod(new ScrollingMovementMethod());

if(textView.getLineCount() > 5)
{
   view.setVisibility(View.VISIBLE);
}

textView.getLineCount()等于0。

还尝试了canScrollHorizontally(int direction)功能,但Google的文档没有说明我应该使用哪个参数。

3 个答案:

答案 0 :(得分:2)

您获得ZERO是因为您尝试在非UI线程中获取结果。

尝试

textView.post(new Runnable() {

    @Override
    public void run() {

    int lineCount = textView.getLineCount();
    if(lineCount >=5)
        textView.setVisibility(View.VISIBLE);
    }
});

答案 1 :(得分:-1)

使用线程获取lineCount:

@Override
public void run() {
    while(textView.getLineCount() == 0){   
    }
    textview_lineCount = textView.getLineCount(); 

}

答案 2 :(得分:-1)

假设您的TextViewScrollView的孩子,并且您希望自动在追加TextView之后滚动它,现在包含更多文字,您可以执行以下操作:

TextView t = (TextView) findViewById(R.id.someTextView);
t.append("Some text");

ScrollView s;
if (s.canScrollVertically(1)) {
    logScroll(s);
}

private void logScroll(final ScrollView s) {
    s.post(new Runnable() {
        @Override
        public void run() {
            s.fullScroll(ScrollView.FOCUS_DOWN);
        }
    });
}
相关问题