0

首先,我是 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

4 回答 4

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()在 else 条件下调用方法。

    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 ();
于 2013-01-29T11:25:10.983 回答
0

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

于 2013-01-29T11:25:14.570 回答
0

您可以简单地在您的活动上调用 finish() 来关闭它。

于 2013-01-29T11:25:54.960 回答
0

如果你想退出你可以使用finish();

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

于 2013-01-29T11:30:52.420 回答