如何让我的应用继续检查?意思是继续检查那里是否有文字
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 :(得分: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
关键字。话虽这么说,我不推荐这样的方法,因为它很可能会影响你的用户界面。我建议你做的是将Focus Lost事件附加到文本字段,并在焦点丢失时进行检查。这将允许您仅在需要时运行检查。
答案 1 :(得分: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);
}
});
答案 2 :(得分:0)
在条件中使用 while循环并使用布尔值true ,在其中编写逻辑,在哪里继续检查使用继续关键字,您要停止检查使用中断关键字。
答案 3 :(得分:0)
我假设您想在修改EditText的文本时执行某些操作。如果是这样,请使用addTextChangedListener
使用它:
word.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do whatever you need to do HERE.
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 4 :(得分:0)
在你的问题中 str1 = word.getText()。toString(); str2 = answer.getText()。toString();
表示word或answer是edittext或textview为其编写TextChanged侦听器。