【发布时间】:2014-05-28 18:47:17
【问题描述】:
我创建了以下主要类,它向用户显示欢迎片段。这个欢迎片段有 2 个按钮:注册和登录。当用户单击注册按钮时,我希望他们路由到注册片段,当他们单击登录按钮时,用户需要转到登录片段。我面临的问题是单击按钮,没有任何反应。 我的主要活动如下所示:
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import android.util.Log;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;
import android.support.v4.widget.DrawerLayout;
import com.parse.ParseObject;
import com.project.iandwe.Adaptor.NavigationDrawerListAdapter;
import com.project.iandwe.Fragments.UserLogIn;
import com.project.iandwe.Fragments.UserSignUp;
import com.project.iandwe.Fragments.WelcomePage;
import com.project.iandwe.Menu.*;
import com.project.iandwe.Navigation.NavigationDrawer;
public class MainActivity extends FragmentActivity implements Communicator {
@Override
protected void onCreate(Bundle savedInstanceState) {
WelcomePage welcomePage = new WelcomePage();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentWelcome,welcomePage);
fragmentTransaction.show(welcomePage);
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
fragmentTransaction.commit();
}
@Override
public void respond(int button) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (button == 1) {
UserSignUp userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
fragmentTransaction.replace(R.id.fragmentISignUp, userSignUp);
fragmentTransaction.show(userSignUp);
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
} else if (button == 2) {
UserLogIn userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
fragmentTransaction.replace(R.id.fragmentLogin, userLogIn);
fragmentTransaction.show(userLogIn);
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
}
fragmentTransaction.commit();
}
我的片段如下所示:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.project.iandwe.Communicator;
import com.project.iandwe.R;
/**
* Created by NathanDrake on 5/28/2014.
*/
public class WelcomePage extends Fragment implements View.OnClickListener {
Button login,signUp;
int buttonFlag =0;
Communicator communicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutInflater;
layoutInflater=inflater.inflate(R.layout.welcome_page,container, false );
/* login = (Button) getActivity().findViewById(R.id.buttonLogin);
signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
communicator = (Communicator) getActivity();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonFlag =1;
communicator.respond(buttonFlag);
}
});
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonFlag =2;
communicator.respond(buttonFlag);
}
});
communicator.respond(buttonFlag); */
return layoutInflater;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
login = (Button) getActivity().findViewById(R.id.buttonLogin);
signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
communicator = (Communicator) getActivity();
login.setOnClickListener(this);
signUp.setOnClickListener(this);
/*
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonFlag =1;
communicator.respond(buttonFlag);
}
});
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonFlag =2;
communicator.respond(buttonFlag);
}
});
communicator.respond(buttonFlag); */
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonLogin:
buttonFlag =1;
break;
case R.id.buttonSignUp:
buttonFlag =2;
break;
}
communicator.respond(buttonFlag);
}
}
Communicator 是一个用于片段间通信的普通接口,如下所示:
public interface Communicator {
public void respond(int button);
}
我尝试了这里给出的方法,但仍然没有来自片段的响应:How to handle button clicks using the xml onClick within Fragments
请看一下,让我知道我在这里缺少什么。
更新:
我更改了我的代码以包含这样的片段引用:
welcomePage = new WelcomePage();
FragmentManager fragmentManager = getSupportFragmentManager();
userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
registerUsingEmail = (RegisterUsingEmail) fragmentManager.findFragmentById(R.id.fragmentRegister);
fragmentTransaction.hide(userLogIn);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
fragmentTransaction.hide(userSignUp);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
fragmentTransaction.hide(registerUsingEmail);
fragmentTransaction.commit();
并更新了我的通讯器响应功能:
@Override
public void respond(int button) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(welcomePage);
fragmentTransaction.remove(welcomePage);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
if (button == 1) {
// userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
fragmentTransaction.remove(userSignUp);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentISignUp, userSignUp);
Toast.makeText(getApplicationContext(),"You are inside user signup Button",Toast.LENGTH_LONG).show();
fragmentTransaction.show(userSignUp);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
fragmentTransaction.hide(userLogIn);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
fragmentTransaction.hide(registerUsingEmail);
} else if (button == 2) {
// userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
fragmentTransaction.remove(userLogIn);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentLogin, userLogIn);
Toast.makeText(getApplicationContext(),"You are inside user login Button",Toast.LENGTH_LONG).show();
fragmentTransaction.show(userLogIn);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
fragmentTransaction.hide(userSignUp);
//fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
// fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
fragmentTransaction.hide(registerUsingEmail);
}
fragmentTransaction.commit();
}
我现在面临的问题是我没有看到现有的欢迎片段被用户签名或用户登录片段替换。我包含了 toast 消息以查看代码是否正在运行,当响应方法为时我看到这些消息由按钮调用,但我没有看到用户界面得到更新。我检查了move fragments 并尝试了那里的所有内容,但没有任何效果
【问题讨论】: