这是我的导航标题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="176dp"
android:background="@color/colorAccent"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="John Doe"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="johndoe@gmail.com" />
</LinearLayout>
如您所见,我使用了假的用户名
、电子邮件
和图片
。
如何检查用户是否登录,然后隐藏/显示登录按钮而不是数据?
如果用户未登录,我想显示“注册/登录”按钮。
我正在使用 firebase
。所以我知道我可以使用 auth 来检查它。
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
//Logged in
} else {
// NOT logged in
}
但我不知道如何/在哪里制定条件以及如何更改它。
1- 如果登录,则显示他的图片/用户名/电子邮件;
2- 如果未登录,则显示登录/注册按钮/链接。
当前代码(遵循彼得的回答)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth.addAuthStateListener(mAuthListener);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
//User login elements
ImageView userAvatar = findViewById(R.id.user_avatar);
TextView userNickname = findViewById(R.id.user_nickname);
TextView userEmail = findViewById(R.id.user_email);
TextView registerLink = findViewById(R.id.link_cadastrar);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MessageFragment()).commit();
navigationView.setCheckedItem(R.id.nav_message);
}
// Listen to user login
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null) {
// NOT LOGGED IN
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
}
else {
// LOGGED IN
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
}
应用程序因以下错误而崩溃:
Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.addAuthStateListener(com.google.firebase.auth.FirebaseAuth$AuthStateListener)
最佳答案
首先您需要检查用户是否登录:
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
//Logged in
} else {
// NOT logged in
}
然后要显示电子邮件/用户名/图片(如果他已登录),您可以使用 setVisibility
为每个小部件添加一个 ID:
android:id="@+id/userProfilePic"
android:id="@+id/email"
android:id="@+id/userName"
然后做:
userProfilePic.setVisibility(View.VISIBLE);
email.setVisibility(View.VISIBLE);
userName.setVisibility(View.VISIBLE);
关于java - 如果用户是否登录,如何检查元素之间的交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51144200/