Java 值更改时显示字符串(值越高,字符越多)
我在考虑动态显示文本,我有一个问题 我有很长的绳子。。。。几千个字符或更多。我有一个随时间增加的值。现在,在开始时,当值为0时,我不希望显示任何内容(到目前为止很简单:)),但随着值的增加,我希望字符串慢慢显示(假设每次“递增”50个字符)。。。最好能达到打字的效果 我的解决方案是在onCreate方法中声明空字符串Java 值更改时显示字符串(值越高,字符越多),java,android,Java,Android,我在考虑动态显示文本,我有一个问题 我有很长的绳子。。。。几千个字符或更多。我有一个随时间增加的值。现在,在开始时,当值为0时,我不希望显示任何内容(到目前为止很简单:)),但随着值的增加,我希望字符串慢慢显示(假设每次“递增”50个字符)。。。最好能达到打字的效果 我的解决方案是在onCreate方法中声明空字符串 string displayedText = ""; 然后,我将调用我的处理程序中等待增量的display函数 private Handler mHandler = new Ha
string displayedText = "";
然后,我将调用我的处理程序中等待增量的display函数
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case VALUE_CHANGE:
value = (int)msg.arg1; // get value from message
mValueView.setText("" + mStepValue); // display value on screen
//here call the function to display text
break;
default:
super.handleMessage(msg);
}
}
};
到目前为止,这是有效的。。。现在(对我来说)有问题的部分是显示文本。可以用不同的时间发送值,从递增到10秒不等(值更改时是随机的)使用延迟消息。没有一个硬负荷的用户界面
private boolean isCanChange = true;
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case VALUE_CHANGE:
value = (int)msg.arg1; // get value from message
mValueView.setText("" + mStepValue); // display value on screen
//here call the function to display text
isCanChange = true;
break;
default:
super.handleMessage(msg);
}
}
};
降低更新速度
public void parseDocument(...)
{
long startParsingTime= System.currentTimeMillis();
while(isParsing) {
// parse
if(isCanChange) {
isCanChange = false;
mHandler.sendEmptyMessageDelayed(VALUE_CHANGE, (System.currentTimeMillis() - startParsingTime)/1000);// update every second
}
}
}
注:可能是你需要投。我手上没有任何IDE。使用
子字符串
方法从完整的字符串
中提取
private String extractText(String input, int length)
{
return input.substring(0, length - 1);
}
在显示文本方法中,您做什么?问题出在哪里?我还没有显示文本的方法,因为我不知道如何从字符串中一次提取一个字符添加到显示文本中。你能把你的问题解释得更清楚一点吗?现在您说您在提取文本方面有问题。你的问题主题说明了另一件事。我有一个文本,我想随着值的增长显示越来越多,因此,例如值为0,当值为2时,我可以从文本中看到0个字符,当值为4时,我可以从文本中看到60个字符,我可以从文本中看到120个字符,等等。我在解释问题时有点困惑。。。接收消息有效:)谢谢:)并且很抱歉混淆了问题的解释。。。英语不是我的母语。没问题。如果答案能解决你的问题,就接受它。
private String extractText(String input, int length)
{
return input.substring(0, length - 1);
}