简体   繁体   English

如何在Android中单击按钮开始新的活动?

[英]How to start new activity on button click in android?

I am trying to go from one activity to another when clicking the 'Login' button, here is my code for button click- 点击“登录”按钮时,我试图从一种活动转到另一种活动,这是我的按钮点击代码,

public void onClick(View v) {
                // TODO Auto-generated method stub

                if(id.getText().toString().equals("ajay")&&pass.getText().toString().equals("sainy"))
                {
                    Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

                }
                else if(id.getText().toString().equals("aj")&&pass.getText().toString().equals("sa"))
                {
                    Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 
                }
                else
                    res.setText("Incorrect Credentials...Retry");
            }
        });

but when I enter correct credentials 'adminhome' or 'userhome' activity is not starting. 但是当我输入正确的凭据时,“ adminhome”或“ userhome”活动未启动。 The same 'MainActivity' is opening again. 相同的“ MainActivity”再次打开。 I think I have problem in following code - 我认为以下代码存在问题-

Intent i = new Intent(MainActivity.this,adminhome.class);
                    MainActivity.this.startActivity(i);

or in, 或在

Intent i = new Intent(MainActivity.this,userhome.class);
                    MainActivity.this.startActivity(i);                 

What is the problem? 问题是什么? Please help, I am learning android. 请帮助,我正在学习android。

The answer is in the comments, but anyway, when anyone of you encounters such behavior: 答案在注释中,但是无论如何,当任何人遇到这样的行为时:

  • Make sure the other activities do not have the same layout, and, so, you will think that the same activity is being open when it is the other activity. 确保其他活动没有相同的布局,因此,您将认为同一活动在其他活动中处于打开状态。 Just double check the layout of the activities. 只需仔细检查活动的布局即可。

you can check and see if the new activity is put inside the manifest or not, as i did below 您可以检查并查看是否将新活动放入清单中,就像我在下面所做的那样

        <activity
        android:name="com.hossa.multitask.activity2"
        android:label="@string/app_name" >
        </activity>

Implement the View.OnClickListener interface and override the onClick method. 实现View.OnClickListener接口,并重写onClick方法。

ImageView btnWatchVideo;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search1);
        ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(this);
    }
 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSearch: {
                Intent intent = new 
             Intent(Search.this,SearchFeedActivity.class);
                startActivity(intent);
                break;
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM