2013-01-24 137 views
0

我正在開發一個具有登錄活動的應用程序。 該視圖有一個電子郵件字段,一個密碼字段和一個按鈕。未觸發的事件

我已經創建了一些偵聽器來捕捉各種可能性,但是其中沒有一個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); 
} 

我創建一個登錄ativity我用默認的Eclipse可以創建的第一次嘗試,但我有同樣的問題了。

這裏有什麼問題? 我錯過了什麼嗎?

RG, 埃裏克

回答

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 

    } 
} 
+0

不,沒有工作。根本沒有事件被解僱。 – Eric

0

想通了。

根據主要活動而不是開始登錄活動的新意圖,我將contentview設置爲登錄佈局。

顯然這是錯誤的。