2012-03-20 77 views
0

我该如何让我的应用程序继续检查?通过含义保持检查是否有在那里如何不断检查android中的文本更改事件?

String str1, str2; 

str1 = word.getText().toString(); 
str2 = answer.getText().toString(); 

if(!(str1.equals("")) && !(str2.equals(""))) 
{ 
    teach.setEnabled(true); 
} 
else 
{ 
    teach.setEnabled(false); 
} 

这里是哪里,我会放在固定的代码,使得它检查和每一件事情我的Java代码文本?请帮忙!

public class TeachmeDialog extends Activity implements OnClickListener { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.teachme); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    Button teach = (Button)findViewById(R.id.btn_teach_send); 
    teach.setOnClickListener(this); 


} 



@Override 
public void onClick(View v) { 
    switch(v.getId()) 
    { 
     case R.id.btn_teach_send: 
     { 
      // Create a new HttpClient and Post Header 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://monaiz.net/get.php"); 

      String responseStr = ""; 

      try { 
       TextView word = (TextView)findViewById(R.id.tv_teach_request); 
       TextView answer = (TextView)findViewById(R.id.tv_teach_response); 

       // Add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
       nameValuePairs.add(new BasicNameValuePair("word", word.getText().toString())); 
       nameValuePairs.add(new BasicNameValuePair("answer", answer.getText().toString())); 
       nameValuePairs.add(new BasicNameValuePair("action", "teach")); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 

       responseStr = EntityUtils.toString(entity); 

      } catch (Exception e) { 
       // TODO Auto-generated catch block 
      } 

      if(responseStr.equals("ok")) 
      { 
       Toast.makeText(getApplicationContext(), "Poco just learned a new word!", Toast.LENGTH_LONG).show(); 
       try { 
        this.finish(); 
       } catch (Throwable e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

}

+0

检查里面是否有文字?但是哪里?你到底想做什么? – 2012-03-20 06:48:57

+0

有问题的问题。请说清楚。发布代码不会**发布问题。 – JoxTraex 2012-03-20 06:57:40

+0

可能的重复问题 - http://stackoverflow.com/questions/3696082/how-to-schedule-my-android-app-to-do-something-every-hour – 2012-03-20 06:59:39

回答

1

使用TextWatcher代替。你的“单词”似乎是一个EditText。所以,你可以不喜欢这个 -

word.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
        teach.setEnabled(true); 
      } 
    }); 
+0

感谢您的帮助,工作。 – user1278696 2012-03-20 07:10:53

0

使用while循环布尔值true的情况下,写要继续检查使用继续关键字,要停止它里面的逻辑,检查使用中断关键字。

+0

这将影响UI ...此外,应用程序将挂.. – aProgrammer 2012-03-20 07:01:04

+0

@amit我可能知道背后的原因。 – 2012-03-20 07:02:09

+0

while(true){}将一直运行,直到达到中断条件为止......但是1.当文本改变时(或发生焦点丢失事件,比文本改变事件有效),OP想要检查,在这种情况下,听众会更合适,如其他答案中所建议的。2. UI therad将挂起,直到循环结束... – aProgrammer 2012-03-20 07:10:36

1

你可以做一些事情,像这样:

String str1, str2; 

while (true) 
{ 
    str1 = word.getText().toString(); 
    str2 = answer.getText().toString(); 

    if(!(str1.equals("")) && !(str2.equals(""))) 
    { 
     teach.setEnabled(true); 
     break; 
    } 
    else 
    { 
     teach.setEnabled(false); 
    } 
} 

这将创建一个循环,将一直下去检查一些条件。如果字符串不为空,它将通过使用break关键字停止。话虽如此,我不推荐这样的方法,因为它很可能会影响你的用户界面。我建议你做的是将焦点丢失事件附加到文本字段,并在焦点丢失时进行检查。这将允许您只在需要时运行检查。

0

在乌尔问题 STR1 = word.getText()的toString()。 str2 = answer.getText()。toString();

表示无论是word还是answer是edittext或textview都为此写入TextChanged监听器。