Java 我如何获得一个按钮来打开另一个活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24610527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:26:58  来源:igfitidea点击:

How do I get a button to open another activity?

javaandroidxml

提问by BionicDroid

I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?

我在我的活动 XML 文件中添加了一个按钮,但无法打开我的其他活动。有人可以告诉我如何一步一步地做到这一点吗?

回答by harveyslash

Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
        startActivity(i);
    }
});

回答by Rudi Kershaw

Using an OnClickListener

使用 OnClickListener

Inside your Activityinstance's onCreate()method you need to first find your Buttonby it's id using findViewById()and then set an OnClickListenerfor your button and implement the onClick()method so that it starts your new Activity.

在您的Activity实例onCreate()方法中,您需要首先Button使用它的 id找到您的方法findViewById(),然后OnClickListener为您的按钮设置一个并实现该onClick()方法,以便它启动您的新Activity.

Button yourButton = (Button) findViewById(R.id.your_buttons_id);

yourButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v){                        
        startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
    }
});

This is probably most developers preferred method. However, there is a common alternative.

这可能是大多数开发人员首选的方法。但是,有一个通用的替代方案。

Using onClick in XML

在 XML 中使用 onClick

Alternatively you can use the android:onClick="yourMethodName"to declare the method name in your Activitywhich is called when you click your Button, and then declare your method like so;

或者,您可以使用android:onClick="yourMethodName"来声明Activity您单击时调用的方法名称Button,然后像这样声明您的方法;

public void yourMethodName(View v){
    startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}

Also, don't forget to declare your new Activityin your manifest.xml. I hope this helps.

另外,不要忘记Activity在您的manifest.xml. 我希望这有帮助。

References;

参考;

回答by Gilad Haimov

A. Make sure your other activity is declared in manifest:

A. 确保您的其他活动在清单中声明:

<activity
    android:name="MyOtherActivity"
    android:label="@string/app_name">
</activity>

All activities must be declared in manifest, even if they do not have an intent filterassigned to them.

所有活动都必须在 manifest 中声明,即使它们没有分配给它们的意图过滤器



B. In your MainActivity do something like this:

B. 在你的 MainActivity 中做这样的事情:

Button btn = (Button)findViewById(R.id.open_activity_button);    

btn.setOnClickListener(new View.OnClickListener() {         
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
        }
});

回答by daniftomas

If you declared your button in the xml file similar to this:

如果您在与此类似的 xml 文件中声明您的按钮:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next activity"
        android:onClick="goToActivity2"
        />

then you can use it to change the activity by putting this at the java file:

然后您可以通过将其放在 java 文件中来使用它来更改活动:

public void goToActivity2 (View view){
    Intent intent = new Intent (this, Main2Activity.class);
    startActivity(intent);
}

Note that my second activity is called "Main2Activity"

请注意,我的第二个活动称为“Main2Activity”

回答by Amol Gursali

Write code on xml file.

在 xml 文件上编写代码。

<Button android:width="wrap_content"
        android:height="wrap_content"
        android:id="@+id/button"
        android:text="Click"/>

Write Code in your java file

在你的java文件中编写代码

Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            startActivity(new Intent(getApplicationContext(),Secondclass.class));
           /* if you want to finish the first activity then just call
            finish(); */
        }
    });

回答by BREI

Apply the following steps:

应用以下步骤:

  1. insert new layout xml in folder layout
  2. rename window2
  3. add new button and add this line: android:onClick="window2"
  1. 在文件夹布局中插入新的布局 xml
  2. 重命名 window2
  3. 添加新按钮并添加这一行: android:onClick="window2"

mainactivity.java

主要活动.java

public void openWindow2(View v) {
        //call window2
        setContentView(R.layout.window2);           
    }
}

回答by BREI

use the following code to have a button, in android studio, open an already existing activity.

使用下面的代码有一个按钮,在android studio中,打开一个已经存在的活动。

Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);

StartButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
    }
});

回答by Matécsa Andrea

I did the same that user9876226 answered. The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"

我做了与 user9876226 回答相同的事情。唯一的区别是,我通常不使用 onClickListener。相反,我在 xml 文件中写了以下内容:android:onClick="open"

openis the function, that is bound to the button. Then just create the function open() in your activity class. When you click on the button, this function will be called :) Also, I think this way is more confortable than using the listener.

open是函数,即绑定到按钮。然后只需在您的活动类中创建函数 open() 。当你点击按钮时,这个函数会被调用:) 另外,我认为这种方式比使用监听器更舒适。