我正在开发一个具有登录活动的应用程序。该视图有一个电子邮件字段、一个密码字段和一个按钮。
我创建了一些侦听器来捕捉各种可能性,但在其中任何一个上都没有触发 onClick 和/或 onTouch 方法。
这是代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.support.v4.app.NavUtils;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
final EditText txtemail;
final EditText txtpassw;
Button btlogin;
btlogin = (Button) findViewById(R.id.btLogin);
txtemail = (EditText) findViewById(R.id.txtEmail);
txtpassw = (EditText) findViewById(R.id.txtPassword);
txtemail.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String txt = txtemail.getText().toString();
if (!hasFocus) {
if (!validateEmail(txt)) {
txtemail.setError("This is not a valid email address!");
} else {
if (!txt.contains("something.com") && !txt.contains("something.nl")) {
txtemail.setError("You cannot use this tool with this email address!");
} else {
if (txt.length() == 0) {
txtemail.setError("This field cannot be blank!");
} else {
txtemail.getNextFocusDownId();
}
}
}
}
}
});
txtpassw.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String txt = txtpassw.getText().toString();
if (!hasFocus) {
if (txt.length() == 0) {
txtpassw.setError("This field cannot be blank!");
}
}
}
});
txtpassw.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
String txt = txtpassw.getText().toString();
if (txt.length() == 0) {
txtpassw.setError("This field cannot be blank!");
return false;
}
if (checkLogin(txtemail.getText().toString(),txtpassw.getText().toString())) {
closeActivity();
return true;
} else {
txtpassw.setError("These credentials are not correct!");
return false;
}
}
return false;
}
});
btlogin.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (checkLogin(txtemail.getText().toString(),txtpassw.getText().toString())) {
closeActivity();
return true;
} else {
txtpassw.setError("These credentials are not correct!");
}
return false;
}
});
btlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (checkLogin(txtemail.getText().toString(),txtpassw.getText().toString())) {
closeActivity();
} else {
txtpassw.setError("These credentials are not correct!");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean validateEmail(String email) {
Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
public void closeActivity() {
NavUtils.navigateUpFromSameTask(this);
}
我第一次尝试创建登录活动时,我使用了 Eclipse 可以创建的默认登录活动,但在那里我也遇到了同样的问题。
这里有什么问题?我错过了什么?
rg,埃里克