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

时间:2015-07-03 06:02:51

标签: java android android-studio

我是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 个答案:

答案 0 :(得分:0)

以上代码的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){}
    }
   }
}

我希望这有帮助...