java 我如何获得一个按钮来在 android studio 中打开一个新活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37222305/
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-11-03 02:18:23  来源:igfitidea点击:

How do i get a button to open a new activity in android studio?

javaandroidandroid-studio

提问by user6333114

I'd seen this asked in many, many places and have tried to follow the instructions given to no avail. I dont know if the questions are old, i'm doing things incorrectly or if my android studio program isnt working. What i want to do is to onlyopen a new activity when a button is clicked. I'm very new to developing android applications.

我在很多很多地方都看到过这个问题,并试图按照给出的说明进行操作,但无济于事。我不知道这些问题是否陈旧,我做错了或者我的 android studio 程序不工作。我想要做的是在单击按钮时打开一个新活动。我对开发 android 应用程序很陌生。

I've recently tried to follow the answer provided here: How do I get a button to open another activity in Android Studio?

我最近尝试按照此处提供的答案进行操作:How do I get a button to open another Activity in Android Studio?

OnClick on the button is called "goTutorials" My original activity is called home (Not mainActivity) The new one is called tutorials.

OnClick按钮叫“goTutorials” 我原来的activity叫home(不是mainActivity)新的叫tutorials。

This is what i added from trying to follow the link above: In home's java file:

这是我尝试按照上面的链接添加的内容:在 home 的 java 文件中:

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

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

In manifest file:

在清单文件中:

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

回答by Omal Perera

Method 1

方法一


Use the onclickattribute in XML(so that whenever you click the button, defined method will trigger)


使用onclickXML 中的属性(这样无论何时单击按钮,都会触发定义的方法)

Step 1. - go to the XML where you button is(activity_home) & add

步骤 1. - 转到按钮所在的 XML ( activity_home) 并添加

<Button
............
android:onClick="gotoTutorial"/>

Step 2. - then go to the home.Java& add following

第 2 步。 - 然后转到home.Java& 添加以下内容

public void gotoTutorial(View v){
    Intent tutorialPage = new Intent (this, tutorials.class);
    startActivity(tutorialPage);
}


Method 2

方法二

Use the setOnClickListener

使用 setOnClickListener

Step 1. -

步骤1。 -

 //create the link to the button in the interface
 btn_tutorial = (Button)findViewById(R.id.tutorial_button); 

 btn_tutorial.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            Intent tutorialPage = new Intent (this, tutorials.class);
            startActivity(tutorialPage);
        }
}

回答by nishant

I have created a sample app please see the code below. You need to specify information of all activities in Manifest file.

我已经创建了一个示例应用程序,请参阅下面的代码。您需要在 Manifest 文件中指定所有活动的信息。

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nextech.startnewactivity">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TutorialsActivity"
        android:label="@string/title_activity_tutorials"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>

My Launcher activity is MainActivity.java

我的启动器活动是 MainActivity.java

package com.nextech.startnewactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button startTutorials = (Button)findViewById(R.id.startTutorials);
    startTutorials.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent tutorialsActivityIntent = new Intent(MainActivity.this,TutorialsActivity.class);
            MainActivity.this.startActivity(tutorialsActivityIntent);

        }
    });
}

@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.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

activity_main.xml

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.MainActivity"
tools:showIn="@layout/activity_main">

<TextView android:id="@+id/welcomeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:layout_centerHorizontal="true"
    android:text="Hello World! This is main activity!" />

<Button android:id="@+id/startTutorials"
    android:layout_below="@+id/welcomeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Start Tutorials"/>
</RelativeLayout>

TutorialsActivity.java

教程Activity.java

package com.nextech.startnewactivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class TutorialsActivity extends AppCompatActivity {

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

}

activity_tutorials.xml

活动教程.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.TutorialsActivity"
tools:showIn="@layout/activity_tutorials">

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is tutorial activity"
    android:layout_centerInParent="true"/>
</RelativeLayout>

I hope it helps.

我希望它有帮助。

回答by Yoshi_64

Your manifest file is not displaying, but make sure you add the activity in your manifest AndroidManifest.xmlin the applicationfield. Here's an example from Google: Link

您的清单文件未显示,但请确保在您的清单AndroidManifest.xml中的application字段中添加活动。这是谷歌的一个例子:链接

<application ... >
...
<activity
    android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>

Otherwise, the method you're using should be correct. You create an intent to the class of the Activity you want to start.

否则,您使用的方法应该是正确的。您为要启动的 Activity 的类创建意图。

Intent intent = new Intent(this, tutorials.class);
startActivity(intent);

Make sure you have the necessary methods such as onCreate(Bundle savedInstance)Overrided.

确保您拥有必要的方法,例如onCreate(Bundle savedInstance)Overrided。

Note that in the sample above android:name=""is your activity's full path name to the class (without .class/.java) and label is going to be the action toolbar string you'll see. In the <meta-data/>section you can alter the android:value=""to the starting activity's name so that will need to change based on what you call it.

请注意,在上面的示例中,android:name=""您的活动的类的完整路径名(不带 .class/.java),而 label 将是您将看到的操作工具栏字符串。在该<meta-data/>部分中,您可以更改android:value=""起始活动的名称,以便需要根据您的称呼进行更改。

Finally, make sure it has an XML view to go with it, as it gets inflated in the onCreate(Bundle savedInstance)call.

最后,确保它有一个 XML 视图,因为它会在onCreate(Bundle savedInstance)调用中膨胀。

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

}

回答by Mrugesh Thaker

inside your activity file write this

在您的活动文件中写下这个

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

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

回答by Ruan

XML

XML

   <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_send"
          android:onClick="sendMessage" />

.Java class

.Java 类

  /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, YourActivityYouWantToOpen.class);
        startActivity(intent);
    }