我刚刚开始学习,请多多包涵。
我想不断检查我的 TextView 是否等于“tata”,然后检查 doStuff(),但不知道该怎么做...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_new_game);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final TextView tv = (TextView) findViewById(R.id.editText2);
// SyllableButtonListener appends TextView only if
// button.getText().equals("ta")
button1.setOnClickListener(new SyllableButtonListener(tv, "ta", button1));
button2.setOnClickListener(new SyllableButtonListener(tv, "ta", button2));
button3.setOnClickListener(new SyllableButtonListener(tv, "ta", button3));
// how to do it: ???
// if(tv.getText.equals("tata")){ doStuff() }
}
任何类型的循环都不起作用。我尝试过使用 Thread,但效果不佳;)
最佳答案
将 TextChangedListener
添加到您的 EditText
//considering it as a EditText instead of Textview since you would know when textView text changes to "tata"
新代码
final EditText editText = (EditText) findViewById(R.id.editText2);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().equals("tata")) {
//do stuff
}
}
});
关于java - 如何等待 TextView 中的特定字符串继续执行下一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22244133/