0

我试图让当用户登录时,当他进入我的应用程序的帐户设置活动时,他将通过 FrameLayout 看到他的用户名,但如果他没有登录,他将看到一个登录按钮。我做了一个 PreferenceData 类来处理这个我相信我设置它是正确的,但是当我登录时,我仍然看到我的登录按钮而不是我制作的 FrameLayout。如果有帮助,我正在使用数据库来存储用户帐户。

这是我的代码

    package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

    public class AccountSettings extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.accountsettings);

            SharedPreferences appPref =
                    getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_Preferences", MODE_PRIVATE);

            String loggedin = PreferenceData.getUserLoggedInStatus(true);




            Button LoginAS = (Button)findViewById(R.id.LoginAS);
            Button Done = (Button)findViewById(R.id.done);
            FrameLayout accountframe = (FrameLayout)findViewById(R.id.AccountFrameLayout);
            TextView accounttv = (TextView)findViewById(R.id.AccountTextView);

            DBAdapter db = new DBAdapter(this);
            db = db.open();



            accountframe.setVisibility(View.GONE);
            LoginAS.setVisibility(View.GONE);


            Done.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent Intent = new Intent(AccountSettings.this,activity_main.class);
                    startActivity(Intent);
                }
            });


            if (accountframe.equals(loggedin))
            {
                accountframe.setVisibility(View.VISIBLE);
                accounttv.setText((CharSequence) appPref);

            } else {

                accountframe.setVisibility(View.GONE);
                LoginAS.setVisibility(View.VISIBLE);
                LoginAS.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity (new Intent ("com.fullfrontalgames.numberfighter.Login")); 

                    }
                });

            }







        }

    }



    PreferenceData Class

    package com.fullfrontalgames.numberfighter;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.view.View.OnClickListener;


public class PreferenceData {
    static final String PREF_LOGGEDIN_USERNAME = "logged_in_username";
    static final String PREF_USER_LOGGEDIN_STATUS = "logged_in_status";

    public static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }


    public static void setLoggedInUsername(Context ctx, String Username)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_LOGGEDIN_USERNAME, Username);
        editor.commit();
    }

    public static String getLoggedInUsername(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_LOGGEDIN_USERNAME, "");
    }

    public static void setUserLoggedInStatus(Context ctx, boolean status)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putBoolean(PREF_USER_LOGGEDIN_STATUS, status);
        editor.commit();
    }

    public static boolean getUserLoggedInStatus(Context ctx)
    {
        return getSharedPreferences(ctx).getBoolean(PREF_USER_LOGGEDIN_STATUS, false);
    }

    public static void clearLoggedInUsername(Context ctx)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.remove(PREF_LOGGEDIN_USERNAME);
        editor.remove(PREF_USER_LOGGEDIN_STATUS);
        editor.commit();
    }


    public static void setUserLoggedInStatus(OnClickListener onClickListener,
            boolean status) {
        // TODO Auto-generated method stub

    }


    public static String getUserLoggedInStatus(boolean b) {
        // TODO Auto-generated method stub
        return null;
    }



    }
4

2 回答 2

1

您正在查看(FrameLayout):

FrameLayout accountframe = (FrameLayout)findViewById(R.id.AccountFrameLayout);

并将其与字符串进行比较:

if (accountframe.equals(loggedin))

你能解释一下你想做什么吗?loggedin字符串存储在哪里?

我认为应该是:

if ((CharSequence) appPref.toString().equals(loggedin))
{
...
}

编辑:

如果用户登录或未登录则返回:

String loggedin = PreferenceData.getUserLoggedInStatus(true);

那么你应该写这样的东西:

 if (loggedin.equals("true"))
 {
     accountframe.setVisibility(View.VISIBLE);
     accounttv.setText((CharSequence) appPref);
 }

Second edit: You are getting a Boolean parameter:

 public static boolean getUserLoggedInStatus(Context ctx)
 {
    return getSharedPreferences(ctx).getBoolean(PREF_USER_LOGGEDIN_STATUS, false);
 }

So you should store it into a Boolean parameter, and pass your activity context or this:

boolean loggedin = PreferenceData.getUserLoggedInStatus(YouactivityName.this);

if (loggedin == true)
{
    accountframe.setVisibility(View.VISIBLE);
    accounttv.setText((CharSequence) appPref);
}
于 2013-03-31T01:35:55.687 回答
1
boolean loggedin = PreferenceData.getUserLoggedInStatus(this);

if (loggedin == true)
 {
     accountframe.setVisibility(View.VISIBLE);
     accounttv.setText((CharSequence) appPref);
 }

try this, Hope this helps you.

于 2013-03-31T01:53:37.863 回答