Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
示例和说明:Android(Studio)登录活动模板生成的活动_Android_Login_Android Styles - Fatal编程技术网

示例和说明:Android(Studio)登录活动模板生成的活动

示例和说明:Android(Studio)登录活动模板生成的活动,android,login,android-styles,Android,Login,Android Styles,我想在我的应用程序中实现一个登录表单,所以我尝试使用Android Studio向导生成的代码来创建一个新的登录表单类型的活动。我认为Eclipse生成的代码几乎相同 不幸的是,生成的代码没有提供预期的结果:我创建了一个很好的简单登录表单,但是使用正确或错误的密码,它不会从登录表单中移动 我还注意到没有创建“注册”表单 看了一会儿,分析了代码,我终于让它工作了:) 请参阅下面我的回答。步骤1:成功登录并进入主要活动 要使登录活动在使用错误的用户/密码时失败,并在成功时转到主活动,您需要对生成的代

我想在我的应用程序中实现一个登录表单,所以我尝试使用Android Studio向导生成的代码来创建一个新的登录表单类型的活动。我认为Eclipse生成的代码几乎相同

不幸的是,生成的代码没有提供预期的结果:我创建了一个很好的简单登录表单,但是使用正确或错误的密码,它不会从登录表单中移动

我还注意到没有创建“注册”表单

看了一会儿,分析了代码,我终于让它工作了:)

请参阅下面我的回答。

步骤1:成功登录并进入主要活动 要使登录活动在使用错误的用户/密码时失败,并在成功时转到主活动,您需要对生成的代码进行以下更正:

AndroidManifest.xml

将以下代码从主活动移动到LoginActivity部分:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
然后在
onPostExecute
方法中,在
finish()之后添加一个新的意图

现在,使用以下
user:password
凭据之一应该可以成功登录:

  • foo@example.com:你好
  • bar@example.com:世界
其他
user:password
try应说明密码不正确并停留在登录页面

步骤2:允许注册,将登录存储到数据库中,并检查凭据vs DB 现在我们将从数据库(SQLite)而不是静态变量获取登录信息。这将允许我们在设备上注册1个以上的用户

首先,创建一个新的
User.java
类:

package com.clinsis.onlineresults.utils;

/**
 * Created by csimon on 5/03/14.
 */
public class User {
    public long userId;
    public String username;
    public String password;

    public User(long userId, String username, String password){
        this.userId=userId;
        this.username=username;
        this.password=password;
    }

}
然后创建或更新您的SQLite helper(
DBTools.java,在我的例子中是
)类:

添加新变量:

private User myUser;
删除
DUMMY\u凭证
变量声明

attemptLogin
方法中,在调用
UserLoginTask
时添加上下文:

mAuthTask = new UserLoginTask(email, password, this);
用以下代码替换内部UserLoginTask类:

/**
     * Represents an asynchronous login/registration task used to authenticate
     * the user.
     */
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

    private final String mEmail;
    private final String mPassword;
    private final Context mContext;

    UserLoginTask(String email, String password, Context context) {
        mEmail = email;
        mPassword = password;
        mContext= context;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        DBTools dbTools=null;
        try{
            dbTools = new DBTools(mContext);
            myUser = dbTools.getUser(mEmail);

            if (myUser.userId>0) {
                // Account exists, check password.
                if (myUser.password.equals(mPassword))
                    return true;
                else
                    return false;
            } else {
                myUser.password=mPassword;
                return true;
        }
        } finally{
            if (dbTools!=null)
                dbTools.close();
        }
        // return false if no previous checks are true
        return false;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            if (myUser.userId>0){
                finish();
                Intent myIntent = new Intent(LoginActivity.this,ReportListActivity.class);
                LoginActivity.this.startActivity(myIntent);
            } else {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case DialogInterface.BUTTON_POSITIVE:
                                DBTools dbTools=null;
                                try{
                                    finish();
                                    dbTools = new DBTools(mContext);
                                    myUser=dbTools.insertUser(myUser);
                                    Toast myToast = Toast.makeText(mContext,R.string.updatingReport, Toast.LENGTH_SHORT);
                                    myToast.show();
                                    Intent myIntent = new Intent(LoginActivity.this,ReportListActivity.class);
                                    LoginActivity.this.startActivity(myIntent);
                                } finally{
                                    if (dbTools!=null)
                                        dbTools.close();
                                }
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                mPasswordView.setError(getString(R.string.error_incorrect_password));
                                mPasswordView.requestFocus();
                                break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
                builder.setMessage(R.string.confirm_registry).setPositiveButton(R.string.yes, dialogClickListener)
                        .setNegativeButton(R.string.no, dialogClickListener).show();
            }
        } else {
            mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
        showProgress(false);
    }
}
我希望我没有忘记任何事情。。。对我来说效果不错:D

如果数据库中没有电子邮件,它将建议注册,否则它将对照密码检查电子邮件


用Android玩得开心:D

真是一个很好的解释!但是为什么不在
onUpgrade
活动中使用此代码:
db.execSQL(“如果存在,则删除表”+FeedEntry.TABLE\u NAME);onCreate(db)
?@Stefano:onUpgrade表示用户已经安装了一个版本,其中很可能包含数据。我不想删除用户数据,因为我进行了升级。这就是为什么我首先检查版本…我唯一感到困惑的是像db这样的类,所有这些类都应该单独创建吗?
import android.widget.Toast;
import com.clinsis.onlineresults.utils.DBTools;
import com.clinsis.onlineresults.utils.User;
private User myUser;
mAuthTask = new UserLoginTask(email, password, this);
/**
     * Represents an asynchronous login/registration task used to authenticate
     * the user.
     */
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

    private final String mEmail;
    private final String mPassword;
    private final Context mContext;

    UserLoginTask(String email, String password, Context context) {
        mEmail = email;
        mPassword = password;
        mContext= context;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        DBTools dbTools=null;
        try{
            dbTools = new DBTools(mContext);
            myUser = dbTools.getUser(mEmail);

            if (myUser.userId>0) {
                // Account exists, check password.
                if (myUser.password.equals(mPassword))
                    return true;
                else
                    return false;
            } else {
                myUser.password=mPassword;
                return true;
        }
        } finally{
            if (dbTools!=null)
                dbTools.close();
        }
        // return false if no previous checks are true
        return false;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            if (myUser.userId>0){
                finish();
                Intent myIntent = new Intent(LoginActivity.this,ReportListActivity.class);
                LoginActivity.this.startActivity(myIntent);
            } else {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case DialogInterface.BUTTON_POSITIVE:
                                DBTools dbTools=null;
                                try{
                                    finish();
                                    dbTools = new DBTools(mContext);
                                    myUser=dbTools.insertUser(myUser);
                                    Toast myToast = Toast.makeText(mContext,R.string.updatingReport, Toast.LENGTH_SHORT);
                                    myToast.show();
                                    Intent myIntent = new Intent(LoginActivity.this,ReportListActivity.class);
                                    LoginActivity.this.startActivity(myIntent);
                                } finally{
                                    if (dbTools!=null)
                                        dbTools.close();
                                }
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                mPasswordView.setError(getString(R.string.error_incorrect_password));
                                mPasswordView.requestFocus();
                                break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
                builder.setMessage(R.string.confirm_registry).setPositiveButton(R.string.yes, dialogClickListener)
                        .setNegativeButton(R.string.no, dialogClickListener).show();
            }
        } else {
            mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
        showProgress(false);
    }
}
<string name="confirm_registry">Email not found. You want to create a new user with that email and password?</string>
<string name="yes">Yes</string>
<string name="no">No</string>