主要活动上的Android登录框

时间:2013-01-29 11:21:24

标签: java android login

首先,我是Android的新手并为任何不合理的事道道歉。

我在这里要做的是显示主要活动并显示AlertDialog,询问短密码(完整用户名和密码将保存在首选项中)。如果密码不匹配,我需要找到退出应用程序的方法,否则,加载主要活动。

到目前为止,这是我的代码:

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);
}
}

4 个答案:

答案 0 :(得分:1)

试试这可能对你有所帮助。你可以使用这个意图。

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 ();

或 如果这是您在应用中的第一个活动,请在其他情况下调用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 ();

答案 1 :(得分:0)

嗯,在Android中确实没有“退出应用程序”。但是,如果您真的想提供这种功能,我猜您可以通过Intent将用户引导到主屏幕(参见Intent)。

答案 2 :(得分:0)

您只需在活动上调用finish()即可关闭它。

答案 3 :(得分:0)

如果要退出,可以使用finish();

关于存储密码的共享首选项,我建议你阅读这篇文章: What is the most appropriate way to store user settings in Android application