代码之家  ›  专栏  ›  技术社区  ›  Mithun Sreedharan Kuldeep Modi

Android应用程序登录

  •  5
  • Mithun Sreedharan Kuldeep Modi  · 技术社区  · 15 年前

    有人能帮我推荐一些好的教程吗? 我想构建一个带有仪表板和登录屏幕的简单应用程序。首次要求用户登录屏幕。登录检查是通过对远程PHP脚本的后调用来执行的。用户登录后,应将其重定向到仪表板。一旦用户关闭并重新打开应用程序,应将其重定向到登录屏幕。

    我知道如何创建表单以及如何发布,在基于用户角色切换布局的领域需要帮助,以及如何导入/扩展类,例如,我更喜欢单独的登录(活动)类。但是这个登录类需要导入到main(main应该扩展活动)

    4 回复  |  直到 8 年前
        1
  •  2
  •   Fredrik Wallenius    15 年前

    我不确定是否完全理解您的问题,但在开始和结束活动时,本教程非常好:

    http://developerlife.com/tutorials/?p=302

    如果您希望用户像您所说的那样,在解决方案中的后台应用程序后返回到该应用程序时,重新定向到登录屏幕,则在您的主活动中捕获onstop()事件。此事件在用户离开应用程序时激发。

    如果您进一步解释“但是这个登录类需要导入到main”这一行,我也可以回答这个问题。

        2
  •  19
  •   Minion baloo    14 年前
        3
  •  2
  •   Timothy E. Johansson    12 年前

    我已经为 UserApp 这将处理大多数用户身份验证,如登录、注册、会话、用户配置文件、社交登录等。

    https://github.com/userapp-io/userapp-android

    由于它是开放源码的,您可以根据自己的需要修改它,并连接到自己的服务。

    登录表单将放置在扩展 AuthFragment ,像这样:

    public class LoginFragment extends AuthFragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_signup, container, false);
    
            // Setup the login form with bindings to UserApp
            super.setupLoginForm(view, R.id.login, R.id.password, R.id.login_button);
    
            return view;
        }
    
        @Override
        public Boolean onLoginStart(String login, String password, Boolean isSocialLogin) {
            // Show loader when waiting for server
            getView().findViewById(R.id.login_form).setVisibility(View.GONE);
            getView().findViewById(R.id.login_status).setVisibility(View.VISIBLE);
    
            // Return true to complete the login
            return true;
        }
    
        @Override
        public void onLoginCompleted(Boolean authenticated, Exception exception) {
            // Hide the loader
            getView().findViewById(R.id.login_form).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.login_status).setVisibility(View.GONE);
    
            if (exception != null) {
                // Show an error message
                ((TextView) getView().findViewById(R.id.error_text)).setText(exception.getMessage());
            } else {
                // Clear the message
                ((TextView) getView().findViewById(R.id.error_text)).setText("");
            }
        }
    }
    

    以及 LoginFragment 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <!-- Progress -->
        <LinearLayout
            android:id="@+id/login_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:visibility="gone" >
    
            <ProgressBar
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp" />
    
            <TextView
                android:id="@+id/login_status_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:fontFamily="sans-serif-light"
                android:text="@string/login_progress_signing_in"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    
        <!-- Login form -->
        <ScrollView
            android:id="@+id/login_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <LinearLayout 
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >
    
                <EditText
                    android:id="@+id/login"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:hint="@string/username"
                    android:maxLines="1"
                    android:singleLine="true" />
    
                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:hint="@string/password"
                    android:maxLines="1"
                    android:singleLine="true" />
    
                <Button
                    android:id="@+id/login_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:paddingLeft="32dp"
                    android:paddingRight="32dp"
                    android:text="@string/login" />
    
                <Button
                    android:id="@+id/facebook_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:paddingLeft="32dp"
                    android:paddingRight="32dp"
                    android:text="@string/facebook_login" />
    
                <Button
                    android:id="@+id/show_signup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:paddingLeft="32dp"
                    android:paddingRight="32dp"
                    android:text="@string/signup" />
    
                <TextView
                    android:id="@+id/error_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textAppearance="?android:attr/textAppearanceSmall" />
    
            </LinearLayout>
        </ScrollView>
    
    </LinearLayout>
    

    然后将它与另一个包含“仪表板”的片段一起添加到主活动的布局中:

    <RelativeLayout ... >
        <fragment
            android:id="@+id/loginFragment"
            android:name="com.example.demo.LoginFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    
        ...
    </RelativeLayout>
    

    这只是简单地展示了如何使用这个库。有关如何使用它的详细信息,请参阅文档。还可以看看GitHub上的演示应用程序。

    如接受答案中所述;要在应用程序关闭时注销用户,请在 onStop() 在你的主要活动和呼叫中的事件 session.logout() ,像这样:

    UserApp.Session session;
    session.logout();
    
        4
  •  0
  •   viv    15 年前

    如果你知道一点php,mysql,并且可以创建一个php文件来验证登录凭证,那么这将很有帮助:

    http://www.androidsnippets.org/snippets/36/index.html

    推荐文章