我有一个包含2个按钮的活动 - 登录和注册。这两个都有一个链接的活动(基于片段的选项卡式活动),其中包含登录/注册表单。
现在,点击按钮后,我无法导航到特定标签页。即,如果单击注册按钮,则应打开基于注册片段的选项卡,如果单击登录按钮,则应打开基于登录片段的选项卡。
我如何实现这一目标?
以下是我各自行动的代码。
主页Activity.java -
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
findViewById(R.id.signup_btn).setOnClickListener(listener_signup_btn);
findViewById(R.id.signin_btn).setOnClickListener(listener_signin_btn);
}
View.OnClickListener listener_signup_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent(HomeActivity.this, SignupActivity.class);
//startActivity(intent);
}
};
View.OnClickListener listener_signin_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, HomeTaberActivity.class);
startActivity(intent);
}
};
}
HomeTaberActivity.java -
public class HomeTaberActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private TabLayout hometabLayout;
private ViewPager homeviewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hometaber);
hometabLayout = (TabLayout) findViewById(R.id.hometabLayout);
hometabLayout.addTab(hometabLayout.newTab().setText("Sign In"));
hometabLayout.addTab(hometabLayout.newTab().setText("Sign Up"));
hometabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
homeviewPager = (ViewPager) findViewById(R.id.homepager);
HomePager adapter = new HomePager(getSupportFragmentManager(), hometabLayout.getTabCount());
adapter.Initialise(new LoginActivity(),new SignupActivity());
adapter.addstring("Sign In"); adapter.addstring("Sign Up");
homeviewPager.setAdapter(adapter);
hometabLayout.setupWithViewPager(homeviewPager);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
homeviewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
答案 0 :(得分:2)
您可以使用
STATIC
方法。 putExtra/getIntExtra
方法。检查 Vishva Dave's
答案。第一路
public static ViewPager homeviewPager; //GLOBAL
public static HomePager adapter; //GLOBAL
adapter = new HomePager(getSupportFragmentManager(), hometabLayout.getTabCount()); //onCreate section
public static void _openSIGNIN() // Create this static method
{
homeviewPager.setCurrentItem(0);
}
public static void _openSIGNUP() // Create this static method
{
homeviewPager.setCurrentItem(1);
}
<强> HomeTaberActivity 强>
View.OnClickListener listener_signin_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, HomeTaberActivity.class);
startActivity(intent);
_openSIGNIN();
}
};
View.OnClickListener listener_signup_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, HomeTaberActivity.class);
startActivity(intent);
_openSIGNUP();
}
};
答案 1 :(得分:0)
使用intent添加一个值。即登录1和注册2.如果值为1,则检入活动,然后查看当前项目0,如果是2,则查看当前项目1。
Home Activity.java :
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
findViewById(R.id.signup_btn).setOnClickListener(listener_signup_btn);
findViewById(R.id.signin_btn).setOnClickListener(listener_signin_btn);
}
View.OnClickListener listener_signup_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, HomeTaberActivity.class);
intent.putExtra("value", 1);
startActivity(intent);
}
};
View.OnClickListener listener_signin_btn = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, HomeTaberActivity.class);
intent.putExtra("value", 2);
startActivity(intent);
}
};
}
HomeTaberActivity.java :
public class HomeTaberActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private TabLayout hometabLayout;
private ViewPager homeviewPager;
int value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hometaber);
hometabLayout = (TabLayout) findViewById(R.id.hometabLayout);
hometabLayout.addTab(hometabLayout.newTab().setText("Sign In"));
hometabLayout.addTab(hometabLayout.newTab().setText("Sign Up"));
hometabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
homeviewPager = (ViewPager) findViewById(R.id.homepager);
HomePager adapter = new HomePager(getSupportFragmentManager(), hometabLayout.getTabCount());
adapter.Initialise(new LoginActivity(),new SignupActivity());
adapter.addstring("Sign In"); adapter.addstring("Sign Up");
homeviewPager.setAdapter(adapter);
hometabLayout.setupWithViewPager(homeviewPager);
final Intent intent = new Intent(getIntent());
value = intent.getIntExtra("value", -1);
if(value == 1) {
viewpager.setCurrentItem(0);
} else {
viewpager.setCurrentItem(1);
}
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
homeviewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}