java - 主要 Activity 上的 Android 登录框

标签 java android authentication

首先,我是 Android 新手,对于任何不合理的事情深表歉意。

我在这里想做的是显示主要 Activity 并显示一个警报对话框,要求输入短密码(完整的用户名和密码将保存在首选项中)。如果密码不匹配,我需要找到一种退出应用程序的方法,否则加载主 Activity 。

这是迄今为止我的代码:

public class MainActivity extends Activity implements OnClickListener
{
Button  screening;
Button  screeningLog;

@Override
protected void onCreate (Bundle savedInstanceState)
{
    resetPreferences ();
    // Set theme
    setTheme (App.getTheme ());
    setContentView (R.layout.main);
    super.onCreate (savedInstanceState);
    createControlsAndListeners ();

    if (!App.isLoggedIn ())
    {
        final LinearLayout view = new LinearLayout (this);
        final TextView passcodeText = new TextView (this);
        final EditText passcode = new EditText (this);

        passcodeText.setText (R.string.passcode);
        passcode.setHint (R.string.passcode_hint);
        passcode.setInputType (InputType.TYPE_TEXT_VARIATION_PASSWORD);

        view.setOrientation (LinearLayout.VERTICAL);
        view.addView (passcodeText);
        view.addView (passcode);

        AlertDialog.Builder builder = new AlertDialog.Builder (this);
        builder.setTitle ("Enter Passcode.");
        builder.setView (view);
        builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
        {
            public void onClick (DialogInterface dialog, int whichButton)
            {
                if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
                {
                    App.setLoggedIn (true);
                    dialog.dismiss ();
                }
                else
                {
                    Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),
                            App.getDelay ());
                    toast.show ();
                }
            }
        });
        builder.show ();
    }
}

public void resetPreferences ()
{
    PreferenceManager.setDefaultValues (this, R.xml.preferences, false);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences (this);
    App.setServer (preferences.getString ("server", ""));
    App.setUsername (preferences.getString ("username", ""));
    App.setPassword (preferences.getString ("password", ""));
    App.setNightMode (preferences.getBoolean ("night_mode", false));
    App.setDelay (Integer.parseInt (preferences.getString ("delay", "30000")));
}

private void createControlsAndListeners ()
{
    screening = (Button) findViewById (R.main_id.screeningButton);
    screening.setOnClickListener (this);
    screeningLog = (Button) findViewById (R.main_id.screeningLogButton);
    screening.setOnClickListener (this);
}
}

最佳答案

试试这个可能会对你有帮助。您可以使用 Intent 来实现此目的。

AlertDialog.Builder builder = new AlertDialog.Builder (this);
builder.setTitle ("Enter Passcode.");
builder.setView (view);
builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
{
    public void onClick (DialogInterface dialog, int whichButton)
    {
        if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
        {
            App.setLoggedIn (true);
            dialog.dismiss ();
        }
        else
        {
            Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),App.getDelay ());
            toast.show ();
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
});
builder.show ();

或者 如果这是您在应用中的第一个 Activity,则在其他条件下调用 finish() 方法。

    AlertDialog.Builder builder = new AlertDialog.Builder (this);
    builder.setTitle ("Enter Passcode.");
    builder.setView (view);
    builder.setPositiveButton (R.string.login, new DialogInterface.OnClickListener ()
    {
        public void onClick (DialogInterface dialog, int whichButton)
        {
            if (App.get (passcode).equals (App.getPassword ().substring (0, 4)))
            {
                App.setLoggedIn (true);
                dialog.dismiss ();
            }
            else
            {
                Toast toast = Toast.makeText (MainActivity.this, Error.get (Error.AUTHENTICATION),App.getDelay ());
                toast.show ();
                finish();
            }
        }
    });
    builder.show ();

关于java - 主要 Activity 上的 Android 登录框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14581730/

相关文章:

Java类,试图创建一个找到初始值的方法

java - 如何通过数据报套接字发送对象

php - 如果登录,laravel 重定向

java - 当我使用正则表达式有数据时,如何将数据从字符串转换为整数

java - 如何在wordcount hadoop中用逗号、空格、句点(.)、制表符(\t)、括号()、方括号[]和大括号({})字符分隔单词?

android - 使用 Parse.com 登录 Facebook 时出现 NoClassDefFoundError

android - 在 Canvas Android 上绘制多条路径

python - 使用@login_required 装饰器的 django 注册/登录 View

authentication - WebApi ActionFilterAttribute、HttpActionContext 访问用户名(IPrincipal)

java.net.MalformedURLException : unknown protocol: localhost at controller. RestController.addService(RestController.java:62)