Java Android Studio:让一个按钮从一个活动转到另一个使用选项卡式模板创建的活动中的特定选项卡?
我的应用程序首先是使用Android studio提供的选项卡式活动模板创建的。我在该活动中有5个选项卡。每个选项卡都有一个按钮,我可以使用该按钮转到我创建的新活动。但是,我无法在新活动上找到一个按钮,以返回到我第一次单击按钮的选项卡 MainActivity.javaJava Android Studio:让一个按钮从一个活动转到另一个使用选项卡式模板创建的活动中的特定选项卡?,java,android,xml,android-studio,tabs,Java,Android,Xml,Android Studio,Tabs,我的应用程序首先是使用Android studio提供的选项卡式活动模板创建的。我在该活动中有5个选项卡。每个选项卡都有一个按钮,我可以使用该按钮转到我创建的新活动。但是,我无法在新活动上找到一个按钮,以返回到我第一次单击按钮的选项卡 MainActivity.java public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; /** *
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private Button eventPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_main, 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.deleteAccount) {
return true;
}
if (id == R.id.logOut){
Intent myIntent = new Intent(MainActivity.this,LoginActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int noOfTabs;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch(position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
Tab4 tab4 = new Tab4();
return tab4;
case 4:
Tab5 tab5 = new Tab5();
return tab5;
default:
return null;
}
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile";
case 1:
return "Connections";
case 2:
return "Suggestions";
case 3:
return "Events";
case 4:
return "Groups";
}
return null;
}}
SectionsPagerAdapter.java
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private Button eventPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_main, 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.deleteAccount) {
return true;
}
if (id == R.id.logOut){
Intent myIntent = new Intent(MainActivity.this,LoginActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int noOfTabs;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch(position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
Tab4 tab4 = new Tab4();
return tab4;
case 4:
Tab5 tab5 = new Tab5();
return tab5;
default:
return null;
}
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile";
case 1:
return "Connections";
case 2:
return "Suggestions";
case 3:
return "Events";
case 4:
return "Groups";
}
return null;
}}
CreateEventActivity
public class CreateEventActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
}}
我想添加功能的按钮位于activity_create_event.xml上,我需要它将用户带回第四个选项卡中的主活动
谢谢你的帮助 欢迎来到stackoverflow!请花一点时间查看我们的。按后退按钮将带您进入堆栈中的活动,即它将带您进入上一个堆栈activity@arjun_sna我的工具栏中没有后退按钮。在按钮的
onClick()
方法中调用onBackPressed()
,将返回stackoverflow!请花一点时间查看我们的。按后退按钮将带您进入堆栈中的活动,即它将带您进入上一个堆栈activity@arjun_sna我的工具栏中没有后退按钮,请在按钮的方法中调用onBackPressed()