0

我有PreferenceScreen一个“按钮”,单击它后,我需要更改on true 的BooleansharedPreference prefLogin,如果 in ,该值可以跳过我的跳过LoginActivity。但它不工作!意图是的,但是,总是将我从登录活动重定向到主要,原因?“意图主要”,因为如果onCreateloginActivity

我该如何解决它!并且 sharedPreferencestartLogin只能在那段时间内保持活动状态

public class NastaveniaActivity extends PreferenceActivity implements Preference.OnPreferenceClickListener 
{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    findPreference("prefLogin").setOnPreferenceClickListener(this);
}



public boolean onPreferenceClick(Preference preference) 
{
    String key = preference.getKey();

    if(key.equals("prefLogin"))
    {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPrefs.edit();

        editor.putBoolean("preflogin", true);
        editor.commit();

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);

        return true;
    }
    return false;
}
}

和 settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/pref_login">
    <PreferenceScreen
        android:title="@string/pref_prihlasovanie"
        android:summary="@string/pref_prihlasovanie_summary"
        android:key="prefLogin" >
    </PreferenceScreen>
</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_update_setting" >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="prefSendReport"
        android:summary="@string/pref_send_report_summary"
        android:title="@string/pref_send_report" />
</PreferenceCategory>

</PreferenceScreen>

LoginActivity -onCreate如果在设置中单击登录后,我必须跳过它

public class LoginActivity extends Activity 
{
String username;
EditText e_username;
Boolean login;
Boolean startLogin;

String vstupneHeslo;
String vstupnaPrezivka;

private NastaveniaActivity nastavenia;
private DatabaseOp mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    showUserSettings(); 

    mDbHelper = new DatabaseOp(this);
    mDbHelper.open();

    if (username!="")
    {
        if (!startLogin)
        {
            if (!login)
                loginStart();
        }
    }
}

.......

private void showUserSettings() 
{
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    username = sharedPrefs.getString("prefUsername", "NULL");
    login = sharedPrefs.getBoolean("prefSendReport", false);
    startLogin = sharedPrefs.getBoolean("prefLogin", false);

    if (username.equals("NULL"))
        username = "";

    e_username = (EditText)findViewById(R.id.login);
    e_username.setText(username);
}

public void loginStart ()
{
    if (startLogin)
    {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        Editor editor = sharedPrefs.edit();

        editor.putBoolean("prefLogin", false);
        editor.commit();
    }

    startActivity(username);
    LoginActivity.this.finish();
}
}
4

1 回答 1

0

尝试这个..

if(key.equals("prefLogin"))
{
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("preflogin", true);
editor.commit();
}
于 2014-04-09T06:59:10.300 回答