简体   繁体   中英

open new activity with button

here is the code of my xml.

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:layout_gravity="center_horizontal"
            android:text="Make New Account"
            android:onClick="new"
            android:textColor="#E74C3C"
            android:textStyle="bold"
            android:paddingTop="20dp"/>

and here is my code of java.

 public Button btn2;
public void onClick(){
    btn2=(Button)findViewById(R.id.btn2);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,submit.class);
            startActivity(intent);
        }
    });

}

my button is not working yet, even an message will appear after clicking on button that unfortunately application has stopped... what is the reason??

Try this code:-

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    android:layout_gravity="center_horizontal"
    android:text="Make New Account"
    android:textColor="#E74C3C"
    android:textStyle="bold"
    android:paddingTop="20dp"/>

Put this in your activity:-

    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this,submit.class);
            startActivity(intent);
        }
    });

您的代码是完美的奈达。只需在清单文件中添加您的活动名称,如下面的代码,

 <activity android:name=".submit"/>

You have two ways:

Either

1) Set an onClick listener on the button

Or

2) Set an onClick attribute on the button and create a method

Method 1

Xml file

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:layout_gravity="center_horizontal"
        android:text="Make New Account"
        android:textColor="#E74C3C"
        android:textStyle="bold"
        android:paddingTop="20dp"/>

Java File

public Button btn2;
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        Intent intent = new Intent(MainActivity.this,submit.class);
        startActivity(intent);
    }
});

Method 2

Xml file

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    android:layout_gravity="center_horizontal"
    android:onclick="newAccount"
    android:text="Make New Account"
    android:textColor="#E74C3C"
    android:textStyle="bold"
    android:paddingTop="20dp"/>

Java file

public Button btn2;
btn2=(Button)findViewById(R.id.btn2);
public void newAccount(View v) {
        Intent intent = new Intent(MainActivity.this,submit.class);
        startActivity(intent);
    }

The thing is you are calling the onClick function when you have declared the onClick of button as new

Try this :

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:layout_gravity="center_horizontal"
        android:text="Make New Account"
        android:onClick="submit"
        android:textColor="#E74C3C"
        android:textStyle="bold"
        android:paddingTop="20dp"/>

And then in your activity class declare this function:

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

That's it. Hope this helps.

Activity is the most basic Android components is also the most common use of the four components ( Activity , Service , Content Provider , BroadcastReceiver ).

The steps to create an Activity :

  1. Create a new Java class And extends the Activity

  2. Add in the AndroidManifest

     <activity android:name=".ActivityClassName"/> 

    If is to start the interface

     <activity android:name=".ActivityClassName"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> 
  3. Override the onCreate() function and load layout

Note: The Activity of the Java classes generally ends in Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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