避免在点击时关闭布局

时间:2015-03-17 10:51:35

标签: android android-layout login

我有一个问题,我无法解决,我正在尝试创建一个小的登录页面,但只要用户名和密码是正确的,一切都按预期进行,但是当凭据不正确时,当您单击按钮以使屏幕完全变白时。想法?

public class SplashActivity extends Activity{

private TextView usernameEdit;
private TextView passwordEdit;
private GetEsercenteForLogin authTask = null;
private View mLoginStatusView;
private LinearLayout splashLayout;

public void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    setContentView(R.layout.splash_activity);

    usernameEdit = (TextView) findViewById(R.id.username);
    passwordEdit = (TextView) findViewById(R.id.password_login);
    mLoginStatusView = findViewById(R.id.login_status);
    splashLayout = (LinearLayout) findViewById(R.id.splash_layout);

    super.onCreate(savedInstanceState);

 Button buttonEntra = (Button) findViewById(R.id.button_entra);

    buttonEntra.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CharSequence usernameEditText = usernameEdit.getText();
            String username = usernameEditText.toString();
            CharSequence passwordEditText = passwordEdit.getText();
            String password = passwordEditText.toString();
            checkLogin(username, password);

        }
    });
}

public void checkLogin(String username, String password) {

    if (authTask != null) {
        return;
    }


    usernameEdit.setError(null);
    passwordEdit.setError(null);

    boolean cancel = false;
    View focusView = null;

    String mPassword = passwordEdit.getText().toString();
    String mUsername = usernameEdit.getText().toString();

    if (TextUtils.isEmpty(mPassword)) {
        passwordEdit.setError(getString(R.string.error_field_required));
        focusView = passwordEdit;
        cancel = true;
    }
    if (TextUtils.isEmpty(mUsername)) {
        usernameEdit.setError(getString(R.string.error_field_required));
        focusView = usernameEdit;
        cancel = true;
    }else if (!mUsername.contains("@")) {
        usernameEdit.setError(getString(R.string.error_invalid_email));
        focusView = usernameEdit;
        cancel = true;
    }


    if (cancel) {
        focusView.requestFocus();
    } else {
        Log.i(SplashActivity.class.getName(), "isOnline: " + SessionUtility.isOnline(this));

        if(SessionUtility.isOnline(this)){
            showProgress(true);
            authTask = new GetEsercenteForLogin(this) {
                @Override
                protected void onPostExecute(EsercenteBean esercente) {
                    authTask = null;
                    showProgress(false);
                    if (esercente == null || !esercente.getEsitoLogin().equals("true")) {
                        usernameEdit.setError(getString(R.string.error_incorrect_user));
                        usernameEdit.requestFocus();
                        splashLayout.requestLayout();
                    }
                    super.onPostExecute(esercente);
                }

                @Override
                protected void onCancelled() {
                    showProgress(false);
                    super.onCancelled();
                }
            };
            authTask.execute(username, password);
        }else{
            String text = "Nessuna connessione";
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        }
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

        mLoginStatusView.setVisibility(View.VISIBLE);
        mLoginStatusView.animate().setDuration(shortAnimTime)
                .alpha(show ? 1 : 0)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
                    }
                });
    } else {
        // The ViewPropertyAnimator APIs are not available, so simply show
        // and hide the relevant UI components.
        mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);

    }
}

}

0 个答案:

没有答案