有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中创建文本类型本身?(Android Studio 1.2.2)

我是JavaScript新手,正在尝试制作一个应用程序,该应用程序以文本输入本身的动画打开。这是我在另一个问题中发现的自键入文本的JavaScript版本:

var text = "The quick fox jumped over the lazy dog.";
var charCount = text.length;
var currentLetterCount = 0;
var speed = 100; // How fast should it type?
var $input = document.getElementById("someInput");

function writeLetter() {
    var currentText = $input.value;
    var currentLetter = text.charAt(currentLetterCount);
    currentLetterCount++;
    $input.value = currentText + currentLetter;
    if(currentLetterCount == charCount)
        clearInterval(timerId);
}

var timerId = setInterval(writeLetter, speed);

我基本上是想把它转换成Java。请帮忙,谢谢


共 (1) 个答案

  1. # 1 楼答案

    上述代码的Java版本:

    Class LetterDisplay(){
    
    public String text = "The quick fox jumped over the lazy dog.";
    public int charCount = text.length;
    public int currentLetterCount = 0;
    public int speed = 100; // How fast should it type?   
    TextView textarea = (TextView)findViewById(R.id.tv); 
    
    
    public static void main(String[] args){
    
    LetterDisplay mainObj = new LetterDisplay();
    
    Runnable r = new MyThread(mainObj);
    new Thread(r).start();
    
    
    }
    
    public class MyThread implements Runnable {
    
       public MyThread(Object parameter) {
           // store parameter for later user
       }
    
       public void run() {
    
    
         try {
          doRun();
        } finally {
          notifyListeners();
        }
        public doRun(){
        String currentText = textarea.getText();
        char currentLetter = text.charAt(currentLetterCount);
        currentLetterCount++;
        textarea.setText(currentText + currentLetter);
        if(currentLetterCount == charCount)
            Thread.stop(); // this is Evil way to Stop a Thread
      }
    try{ Thread.Sleep(speed); } catch(Exception e){}
        }
       }
    }
    

    我希望这有助于