获取字符串时停止计时器

时间:2019-06-26 11:54:30

标签: android eclipse

我想制作一个计时器应用程序,计算某人输入的时间。

我要执行以下步骤-

  1. 单击EditBox并开始键入时启动计时器

  2. 单击按钮后,文本将显示在TextView中

  3. 在步骤2中单击按钮后,从TextView检索值后计时器立即停止

1 个答案:

答案 0 :(得分:0)

为此,您应该使用TextWatcher。调用TextWatcher’s afterTextChanged()方法来通知您,EditText内某处的文本已更改。

如果更改了文本,并且长度为1(表示开始输入),则长度为0(表示停止输入)。

示例android代码:

messageEditText.addTextChangedListener(new TextWatcher() {

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

public void onTextChanged(CharSequence s, int start, int before, int count) {

}

public void afterTextChanged(Editable s) {

      if (!TextUtils.isEmpty(s.toString()) && s.toString().trim().length() == 1) {

          //Log.i(TAG, “typing started event…”);

          typingStarted = true;

           //send typing started status

      } else if (s.toString().trim().length() == 0 && typingStarted) {

          //Log.i(TAG, “typing stopped event…”);

          typingStarted = false;

           //send typing stopped status
     }
}

});

您可以根据上述值启动和停止计时器。