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を参照)。

于 2013-01-29T11:25:14.570 に答える
0

アクティビティで finish() を呼び出すだけで、アクティビティを閉じることができます。

于 2013-01-29T11:25:54.960 に答える
0

終了したい場合は、finish(); を使用できます。

パスワードを保存するための共有設定については、次の投稿を読むことをお勧めします: Android アプリケーションでユーザー設定を保存する最も適切な方法は何ですか?

于 2013-01-29T11:30:52.420 に答える