如何在Android Studio中获得一个按钮来打开另一个活动?

时间:2021-06-07 07:29:53

Hi I'm new to programing and I'm using the Android Studio to code my Android app. I used to work with Eclipse but I'm trying something new. 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 please?

你好,我是编程新手,我正在使用Android Studio来编写我的Android应用。我在活动中添加了一个按钮。XML文件,我无法打开其他活动。能不能请您一步一步地告诉我怎么做?

8 个解决方案

#1


55  

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 filter assigned to them.

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


B. In your MainActivity do something like this:

在你的主要活动中做这样的事情:

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));
        }
});

#2


13  

Using an OnClickListener

Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.

在活动实例的onCreate()方法中,首先需要使用findViewById()通过它的id找到按钮,然后为按钮设置OnClickListener,并实现onClick()方法,以便它启动新的活动。

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

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

您也可以使用android:onClick=“yourMethodName”在您的活动中声明您单击按钮时调用的方法名称,然后像这样声明您的方法;

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

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

另外,不要忘记在manifest.xml中声明新活动。我希望这可以帮助。

References;

引用;

#3


6  

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”

#4


1  

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);
    }
});

#5


1  

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(); */
        }
    });

#6


1  

Apply the following steps:

使用以下步骤:

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

mainactivity.java

mainactivity.java

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

#7


0  

public void onButtonClick(View view)

公共空间onButtonClick(查看视图)

startActivity(new Intent (Current activity.this, IntentActivity.class));

startActivity(新意图(当前活动。这一点,IntentActivity.class));

#8


0  

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));
    }
});

#1


55  

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 filter assigned to them.

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


B. In your MainActivity do something like this:

在你的主要活动中做这样的事情:

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));
        }
});

#2


13  

Using an OnClickListener

Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.

在活动实例的onCreate()方法中,首先需要使用findViewById()通过它的id找到按钮,然后为按钮设置OnClickListener,并实现onClick()方法,以便它启动新的活动。

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

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

您也可以使用android:onClick=“yourMethodName”在您的活动中声明您单击按钮时调用的方法名称,然后像这样声明您的方法;

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

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

另外,不要忘记在manifest.xml中声明新活动。我希望这可以帮助。

References;

引用;

#3


6  

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”

#4


1  

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);
    }
});

#5


1  

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(); */
        }
    });

#6


1  

Apply the following steps:

使用以下步骤:

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

mainactivity.java

mainactivity.java

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

#7


0  

public void onButtonClick(View view)

公共空间onButtonClick(查看视图)

startActivity(new Intent (Current activity.this, IntentActivity.class));

startActivity(新意图(当前活动。这一点,IntentActivity.class));

#8


0  

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));
    }
});