以定时增量附加滚动文本视图

发布于 2024-10-20 01:02:15 字数 372 浏览 2 评论 0原文

我创建了一个滚动文本视图,并且能够添加文本并滚动得很好。问题是,我试图按时间间隔添加文本,并且用户可以通过点击屏幕底部的两个按钮之一来调整所述间隔。一切都已就位,但无论我做什么,我只能让它在整个持续时间后立即显示所有文本。假设我试图在十秒钟内每半秒添加一次文本。运行它十秒钟没有任何结果,然后一切都会显示出来。我尝试过使用计数器的 for/while 循环并通过系统时间进行跟踪。没有。递归很快就会破坏堆栈,这并不奇怪。使用 wait() 或 Thread.sleep() 不起作用,而且也不是理想的选择,因为按钮需要始终处于活动状态。在私有内部类中创建单独的线程不起作用,因为您无法触摸在另一个线程中创建的视图。尝试在单独的线程中创建自定义视图拒绝工作,原因我尚无法弄清楚。

我到底该怎么做才能实时添加每个条目?

I've created a scrolling textview and have been able to add text and scroll just fine. The thing is, I'm trying to add text at timed intervals and said interval can be adjusted by the user by hitting one of two buttons at the bottom of the screen. Everything is in place, but no matter what I do, I can only get it to display all of the text at once after the whole duration. Say I'm trying to add text every half second for ten seconds. Running it results in nothing for ten seconds, and then everything shows up. I've tried for/while loops with counters and keeping track via system time. Nope. Recursion blows the stack in no time, which wasn't much of a surprise. Using wait() or Thread.sleep() don't work and wouldn't be ideal anyway as the buttons need to be live at all times. Making a separate thread in a private inner class didn't work as you can't touch Views created in another thread. Trying to create a custom View in separate thread refused to work for reasons I can't figure out just yet.

How in the heck would I do this so each entry is added in real time?

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

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

发布评论

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

评论(1

幽梦紫曦~ 2024-10-27 01:02:15

就我而言,它可以很好地与一个 Timer 对象一起自动滚动

timer=new Timer();
scroll.setSmoothScrollingEnabled(true);
scroll.pageScroll(ScrollView.FOCUS_DOWN);
scroll.smoothScrollTo(0, text.getBaseline());
x=text.getBaseline();
Toast.makeText(scoruby.this,"number"+x++, Toast.LENGTH_SHORT).show();

timer.schedule(new TimerTask() {

@Override
public void run() {
    TimerMethod();


}
}, 0,1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.

//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
public void run() {
    x+=5;
    scroll.smoothScrollTo(0, x++);  



}
};

In My case It works Fine for Autoscroling along with one Timer object

timer=new Timer();
scroll.setSmoothScrollingEnabled(true);
scroll.pageScroll(ScrollView.FOCUS_DOWN);
scroll.smoothScrollTo(0, text.getBaseline());
x=text.getBaseline();
Toast.makeText(scoruby.this,"number"+x++, Toast.LENGTH_SHORT).show();

timer.schedule(new TimerTask() {

@Override
public void run() {
    TimerMethod();


}
}, 0,1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.

//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
public void run() {
    x+=5;
    scroll.smoothScrollTo(0, x++);  



}
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文