2013-03-31 30 views
0

我想讓用戶登錄時,當他進入我的應用程序的活動,他將通過FrameLayout看到他的用戶名,但如果他沒有登錄,他會看到一個登錄按鈕。我做了一個PreferenceData類來處理這個問題,我相信我的設置是正確的,但是當我登錄時,我仍然看到我的登錄按鈕,而不是我製作的FrameLayout。如果有幫助,可以使用數據庫來存儲用戶帳戶。如何根據登錄狀態從按鈕切換到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; 
    } 



    } 

回答

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); 
} 

第二個編輯: 你得到一個Boolean參數:

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

所以你應該把它存儲到一個布爾參數,並通過你的活動範圍內或本:

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

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

已登錄字符串來自PreferenceData Activity,我試圖做的是當用戶登錄到他/她的帳戶時,他們將在FrameLayout中看到它們的名稱,它由Imageview,TextView和按鈕組成,但是如果他們沒有登錄,他們只會看到LinearLayout父視圖中的一個按鈕,該按鈕表示Login會導致我的登錄活動。我試圖根據用戶是否登錄來呈現2個不同的視圖。 – Cranosaur

+0

但你正在比較兩種不同的東西......你不能比較查看與字符串,你應該檢查什麼是記錄的用戶字符串,如果它存在,並做相應的行動 –

+0

看到更新的答案。 –

1
boolean loggedin = PreferenceData.getUserLoggedInStatus(this); 

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

試試這個,希望這可以幫助你。

+0

因此,我可以在任何活動中使用您的第一行我有? – Castiblanco

+0

是的,我認爲你的邏輯是這樣的成功登錄後,你正在調用方法setUserLoggedInStatus(上下文ctx,布爾狀態),並將它的真實狀態paining.right? –