我创建了一个登录布局(从 MainActivity 开始)
public class MainActivity extends AppCompatActivity implements View.OnClickListener, BottomNavigationView.OnNavigationItemSelectedListener{
private Button regiBtn;
private Button loginBtn;
private Button forgotBtn;
private EditText loginEmail;
private EditText loginPwd;
public static FirebaseAuth mAuth;
public static FirebaseUser currentUser;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
case R.id.navigation_task:
fragment = new TaskFragment();
break;
case R.id.navigation_leadingboard:
fragment = new LeadingBoardFragment();
break;
case R.id.navigation_home:
fragment = new HomeFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment){
if(fragment!=null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
// check if user is signed in (non-null)
currentUser = mAuth.getCurrentUser();
loginBtn = (Button) findViewById(R.id.loginBtn);
forgotBtn = (Button) findViewById(R.id.forgotBtn);
regiBtn = (Button) findViewById(R.id.regiBtn);
loginEmail = (EditText) findViewById(R.id.loginEmail);
loginPwd = (EditText) findViewById(R.id.loginPwd);
regiBtn.setOnClickListener(this);
loginBtn.setOnClickListener(this);
forgotBtn.setOnClickListener(this);
// check if user is logged in or not
updateUI(currentUser);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.regiBtn:{
Intent intent = new Intent(this, RegisterActivity.class);
this.startActivity(intent);
break;
}
case R.id.loginBtn:{
// perform login authentication function
mAuth.signInWithEmailAndPassword(loginEmail.getText().toString(), loginPwd.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
} else{
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
break;
}
case R.id.forgotBtn:{
// perform find lost password function
Intent intent = new Intent(this, ForgotPwdActivity.class);
startActivity(intent);
break;
}
}
}
public void updateUI(FirebaseUser user){
if(user!=null){
setContentView(R.layout.activity_dashboard);
BottomNavigationView navView = findViewById(R.id.nav_view);
// set default to home fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new HomeFragment())
.commit();
navView.setOnNavigationItemSelectedListener(this);
}
}
}
如您所见,当用户登录后,MainActivity 将其布局更改为底部导航布局,用户可以在其中选择要执行特定操作的部分。
在其中一个 fragment 中,我想执行注销功能
public class ProfileFragment extends Fragment {
private Button logoutBtn;
private Button storeBtn;
private Button updateBtn;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, null);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
logoutBtn = (Button) getView().findViewById(R.id.logoutBtn);
storeBtn = (Button) getView().findViewById(R.id.storeBtn);
updateBtn = (Button) getView().findViewById(R.id.updateProfileBtn);
logoutBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
MainActivity.mAuth.signOut();
getActivity().setContentView(R.layout.activity_main);
}
});
}
}
我想将当前 Activity (应该是 MainActivity 对吗?)布局更改为 activity_main(用户执行登录功能的地方),布局确实发生了变化,但按钮无法正常工作。 布局更改为 activity_main(用户登录)-> activity_dashboard(带 fragment )-> main_activity(当用户从 fragment 注销时) 我想知道我哪里出错了。
最佳答案
the layout changed indeed, but the buttons failed to work.
MainActivity 扩展到 AppCompatActivity
。您应该使用 Intent
打开 MainActivity,而不是创建一个新的 setContentView
。
logoutBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
MainActivity.mAuth.signOut();
Intent i = new Intent(getActivity(),MainActivity.class);
startActivity(i);
getActivity().finish();
}
});
关于java - 将布局改回后按钮无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369271/