事件没有解雇

时间:2013-01-24 00:03:11

标签: android android-layout

我正在开发一个具有登录活动的应用。 该视图有一个电子邮件字段,一个密码字段和一个按钮。

我已经创建了一些侦听器以捕获各种可能性,但是在其中任何一个上都没有触发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, 埃里克

2 个答案:

答案 0 :(得分:0)

请注意在您的活动类中实现onTouchListener和onFocusChangedListener

 public class LoginActivity extends Activity implements OnTouchListener,
      ,OnFocusChangeListener{

 @Override
 public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 return false;
  }

  @Override
  public void onFocusChange(View v, boolean hasFocus) {
  // TODO Auto-generated method stub

  }
 }

答案 1 :(得分:0)

想出来。

从主要活动而不是启动登录活动的新意图,我将contentview设置为登录布局。

显然这是错误的。