如何使用单击按钮从任何活动重定向到下一个活动?

时间:2016-12-29 22:23:17

标签: java android android-studio android-fragments android-intent

我在android studio中制作简单的测验应用程序,首先我创建了一个主要的测验活动,PagerAdapter类和六个片段。现在我想添加登录活动来开始这个页面。当我点击“登录”时按钮,我希望此登录活动重定向到主测验页面。我尝试下面的代码但是当我执行代码时,应用程序仍然启动主测验页面。我将类名添加到manifest.xml。我还能做什么呢?

public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final EditText etName = (EditText)findViewById(R.id.etName);
    final EditText etPassword = (EditText)findViewById(R.id.etPassword);
    Button btnLogin = (Button)findViewById(R.id.btnLogin);
    Button btnRegister = (Button)findViewById(R.id.btnRegister);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent mainQuizPaqe = new Intent(LoginActivity.this, MainQuizActivity.class);
            startActivity(mainQuizPaqe);


        }
    });

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent registerScreen = new Intent(LoginActivity.this, Register.class);
            startActivity(registerScreen);

        }
    });
}}

MainQuizActivity

public class MainQuizActivity extends AppCompatActivity {
ViewPager pager;
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_quiz);

    pager= (ViewPager) findViewById(R.id.view_pager);
    tabLayout= (TabLayout) findViewById(R.id.tab_layout);

    // Fragment manager to add fragment in viewpager we will pass object of Fragment manager to adpater class.
    FragmentManager manager=getSupportFragmentManager();

    //object of PagerAdapter passing fragment manager object as a parameter of constructor of PagerAdapter class.
    PagerAdapter adapter=new PagerAdapter(manager);

    //set Adapter to view pager
    pager.setAdapter(adapter);

    //set tablayout with viewpager
    tabLayout.setupWithViewPager(pager);

    // adding functionality to tab and viewpager to manage each other when a page is changed or when a tab is selected
    pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


}

}

1 个答案:

答案 0 :(得分:2)

没有看到你的清单,我猜你没有设置正确的意图过滤器。确保您设置了意图过滤器,以便为要启动的登录活动设置<activity android:name="LoginActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 标志:

{{1}}

如果未对清单中的活动设置intent过滤器,则不会将其设置为第一个活动。