java - 如何根据登录状态从Button切换到FrameLayout

标签 java android eclipse sharedpreferences android-framelayout

我试图做到这一点,当用户登录时,当他进入我的应用程序的帐户设置 Activity 时,他将通过 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;
    }



    }

最佳答案

您正在查看 View (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);
 }

第二次编辑: 您将获得一个 Boolean 参数:

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

因此,您应该将其存储到 boolean 参数中,并传递您的 Activity 上下文或以下内容:

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

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

关于java - 如何根据登录状态从Button切换到FrameLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725290/

相关文章:

java - 使用 gradle 自定义 JUnit 测试检测

Android Brother SDK标签打印编码-变音符号

android - 当用户保持应用程序打开或处于后台时,Webview 丢失记录的用户 session

android - Toast 未在 Samsung Galaxy S3 上显示(最新更新 4.1.2)

eclipse - 如何在 Indigo (3.7) 上安装 Eclipse PDE

java - Jsoup 从 .text() 中排除 child

java - 如何在 Mac 上设置 gradle

java - 在 JSON 中查找深度嵌套的键/值

java - 尝试使 while 循环正常工作

eclipse - 在 Eclipse 中为一个项目禁用自动 Gradle 构建