如何等待TextView中的特定String继续下一个方法

时间:2014-03-07 07:32:01

标签: java android

我刚刚开始学习,所以请宽容。

我想不断检查我的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,但它运行不正常;)

3 个答案:

答案 0 :(得分:2)

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
          }
        }
    });

答案 1 :(得分:0)

根据您的帖子,您的textview经常更改文字。在按下按钮时获取文本视图实例并检查您的文本。 你必须实现onClick(View v)方法。

public void onClick(View v){
   //here v is the button view.
   //you have to use the textview instance which you have already created in onCreate method
    if(textView.getText().equals("YourText"){
      //do your stuff
     }

}

答案 2 :(得分:0)

你需要che id“tata”字符串包含在textView中 你能尝试一下:

teextView.getText().contains("tata")