2015-07-13 44 views
0

这可能只是一个愚蠢的错误,但我无法修复它。我有一个Google+登录按钮,当用户未登录时显示该按钮。我还有一个退出登录按钮,当用户登录时该按钮即为GONE。除了当我返回活动时, onResume)我可以看到大约一秒钟的红色Google+按钮,并且它隐藏起来并显示退出按钮。我怎样才能删除这一秒钟,我仍然可以看到Google+按钮?为什么我的按钮被显示,而不是快速隐藏onResume

这是我的布局: enter image description here

XML代码:

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/startGameView" 
    android:src="@drawable/play" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/startGameView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="46dp"> 
    <!-- show achievements --> 
    <Button 
     android:id="@+id/show_achievements" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Achievements"/> 

    <!-- show leaderboards --> 
    <Button 
     android:id="@+id/show_leaderboard" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Leaderboard"/> 
</LinearLayout> 

代码的活动:

public class StartActivity extends BaseGameActivity implements View.OnClickListener{ 
private ImageView mPlay; 

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

    mPlay = (ImageView)findViewById(R.id.startGameView); 
    mPlay.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //play animations 
      YoYo.with(Techniques.Pulse) 
        .duration(200) 
        .playOn(findViewById(R.id.startGameView)); 
      Intent intent = new Intent(StartActivity.this, MainActivity.class); 
      startActivity(intent); 
     } 
    }); 
    findViewById(R.id.sign_in_button).setOnClickListener(this); 
    findViewById(R.id.sign_out_button).setOnClickListener(this); 
    findViewById(R.id.show_achievements).setOnClickListener(this); 
    findViewById(R.id.show_leaderboard).setOnClickListener(this); 
    //mSignOutButton = (Button)findViewById(R.id.sign_out_button); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_start, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onSignInFailed() { 
    findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); 
    findViewById(R.id.sign_out_button).setVisibility(View.GONE); 
} 

@Override 
public void onSignInSucceeded() { 
    findViewById(R.id.sign_in_button).setVisibility(View.GONE); 
    findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE); 
} 

@Override 
public void onClick(View view) { 
    if (view.getId() == R.id.sign_in_button) { 
     beginUserInitiatedSignIn(); 
    }else if (view.getId() == R.id.sign_out_button) { 
     signOut(); 
     findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); 
     findViewById(R.id.sign_out_button).setVisibility(View.GONE); 
    }else if (view.getId() == R.id.show_achievements){ 
     Toast.makeText(StartActivity.this,"achivements",Toast.LENGTH_SHORT).show(); 
     startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), 1); 
    }else if(view.getId() == R.id.show_leaderboard){ 
     Toast.makeText(StartActivity.this,"leaderboard",Toast.LENGTH_SHORT).show(); 
     startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
       getApiClient(), getString(R.string.number_of_solved_math_problems_leaderboard)), 2); 
    } 
} 

BaseActivity代码:

public abstract class BaseGameActivity extends FragmentActivity implements 
    GameHelper.GameHelperListener { 

// The game helper object. This class is mainly a wrapper around this object. 
protected GameHelper mHelper; 

// We expose these constants here because we don't want users of this class 
// to have to know about GameHelper at all. 
public static final int CLIENT_GAMES = GameHelper.CLIENT_GAMES; 
public static final int CLIENT_APPSTATE = GameHelper.CLIENT_APPSTATE; 
public static final int CLIENT_PLUS = GameHelper.CLIENT_PLUS; 
public static final int CLIENT_ALL = GameHelper.CLIENT_ALL; 

// Requested clients. By default, that's just the games client. 
protected int mRequestedClients = CLIENT_GAMES; 

private final static String TAG = "BaseGameActivity"; 
protected boolean mDebugLog = false; 

/** Constructs a BaseGameActivity with default client (GamesClient). */ 
protected BaseGameActivity() { 
    super(); 
} 

/** 
* Constructs a BaseGameActivity with the requested clients. 
* @param requestedClients The requested clients (a combination of CLIENT_GAMES, 
*   CLIENT_PLUS and CLIENT_APPSTATE). 
*/ 
protected BaseGameActivity(int requestedClients) { 
    super(); 
    setRequestedClients(requestedClients); 
} 

/** 
* Sets the requested clients. The preferred way to set the requested clients is 
* via the constructor, but this method is available if for some reason your code 
* cannot do this in the constructor. This must be called before onCreate or getGameHelper() 
* in order to have any effect. If called after onCreate()/getGameHelper(), this method 
* is a no-op. 
* 
* @param requestedClients A combination of the flags CLIENT_GAMES, CLIENT_PLUS 
*   and CLIENT_APPSTATE, or CLIENT_ALL to request all available clients. 
*/ 
protected void setRequestedClients(int requestedClients) { 
    mRequestedClients = requestedClients; 
} 

public GameHelper getGameHelper() { 
    if (mHelper == null) { 
     mHelper = new GameHelper(this, mRequestedClients); 
     mHelper.enableDebugLog(mDebugLog); 
    } 
    return mHelper; 
} 

@Override 
protected void onCreate(Bundle b) { 
    super.onCreate(b); 
    if (mHelper == null) { 
     getGameHelper(); 
    } 
    mHelper.setup(this); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    mHelper.onStart(this); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    mHelper.onStop(); 
} 

@Override 
protected void onActivityResult(int request, int response, Intent data) { 
    super.onActivityResult(request, response, data); 
    mHelper.onActivityResult(request, response, data); 
} 

protected GoogleApiClient getApiClient() { 
    return mHelper.getApiClient(); 
} 

protected boolean isSignedIn() { 
    return mHelper.isSignedIn(); 
} 

protected void beginUserInitiatedSignIn() { 
    mHelper.beginUserInitiatedSignIn(); 
} 

protected void signOut() { 
    mHelper.signOut(); 
} 

protected void showAlert(String message) { 
    mHelper.makeSimpleDialog(message).show(); 
} 

protected void showAlert(String title, String message) { 
    mHelper.makeSimpleDialog(title, message).show(); 
} 

protected void enableDebugLog(boolean enabled) { 
    mDebugLog = true; 
    if (mHelper != null) { 
     mHelper.enableDebugLog(enabled); 
    } 
} 

@Deprecated 
protected void enableDebugLog(boolean enabled, String tag) { 
    Log.w(TAG, "BaseGameActivity.enabledDebugLog(bool,String) is " + 
      "deprecated. Use enableDebugLog(boolean)"); 
    enableDebugLog(enabled); 
} 

protected String getInvitationId() { 
    return mHelper.getInvitationId(); 
} 

protected void reconnectClient() { 
    mHelper.reconnectClient(); 
} 

protected boolean hasSignInError() { 
    return mHelper.hasSignInError(); 
} 

protected GameHelper.SignInFailureReason getSignInError() { 
    return mHelper.getSignInError(); 
} 
+1

您可以加入您的BaseGameActivity代码? – pjanecze

+0

我确实添加了BaseActivity的代码。如果您正在寻找重写onResume方法,则此活动不会执行此操作。我已经检查过:/ –

+1

保持按钮在布局中默认隐藏。在onResume中,您只需切换可见性即可。经验应该以这种方式改善。 –

回答

1

如果你克隆(或刷新)近日样品,你会发现,GameHelper和BaseGameActivity不是不再使用。有关于此更改的信息视频:Game On! - The death of BaseGameActivity。如果你只是实现接口来获得状态的回调:GoogleApiClient.ConnectionCallbacksGoogleApiClient.OnConnectionFailedListener你的问题应该消失。

关于如何使用这些接口的更多具体细节是https://developers.google.com/games/services/training/signin

+0

谢谢你的回答。我不知道这两个类已被弃用。我会尝试这种方法:) –

1

正如我在GameHelper class看到,它googleApiClient断开对onStop()并连接上onStart()。这是导致闪烁的按钮。

如果你不想改变GameHelper实施,使一些界面改进,使之更讨人喜欢。

+0

谢谢你的建议。但是,我已经应用了@sushant的建议,并且它完美地工作。 –

+0

哦,我错了,他的建议不起作用:/ –