Android Studio click the button to jump to a new interface
Problem description
First, we have two Java files and the XML files bound to them. Here, take history activity.Java, activity_history.xml and event detail.Java, activity_event_detail.xml as examples. We want to add a button in the history activity interface, and click to jump to the event detail interface.
Add button for HistoryActivity interface
In its corresponding activity_history.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".HistoryActivity"> <Button android:id="@+id/History" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Historical Event" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> </android.support.constraint.ConstraintLayout>
We use the android:id="@+id/History" statement to say that the button's ID is set to History, which is used later when setting the click event.
Add a click event for the History button
In HistoryActivity.java:
package com.example.xff.tm; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.content.Intent; import android.widget.Button; import android.widget.*; public class HistoryActivity extends AppCompatActivity { Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_history); button = (Button)findViewById(R.id.History); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(HistoryActivity.this,EventDetail.class); startActivity(intent); } }); } }
Find the corresponding button through the id of the previously defined button, and set click listening for it. When a click event occurs, jump through Intent.
#Add activity in manifests - > androidmanifest.xml (this step is usually generated automatically after adding click event, which can be checked)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.xff.tm"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".HistoryActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".EventDetail"></activity> </application> </manifest>
EventDetail.java,activity_event_detail.xml
As the jump interface, these two files only need to complete their own functions:
EventDetail.java:
package com.example.xff.tm; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class EventDetail extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event_detail); } }
activity_event_detail.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".EventDetail"> </android.support.constraint.ConstraintLayout>