brief introduction
"Activity" is a component responsible for interacting with users in Android applications, which is equivalent to the JFrame control in Swing. However, the JFrame itself can set the layout manager, that is, it can add components, but the activity can only display the components defined in the layout file through the "setContentView(View)" method
1, How to create an Activity
① Create a new Android Studio project;
② Create an Activity in the new project and name it SecondActivity;
③ Create a corresponding UI interface for SecondActivity, and set the layout page through setContView();
④ Complete the registration of Activity.
1. Layout documents
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login_back"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="74dp" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:layout_marginStart="-4dp" android:layout_marginEnd="4dp" android:layout_marginBottom="550dp" android:backgroundTint="#D9E0E4" android:text="Please select login method:" android:textColor="#EAE4E4" android:textSize="24sp" /> <Button android:id="@+id/button1" android:layout_width="240dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginStart="82dp" android:layout_marginEnd="89dp" android:layout_marginBottom="424dp" android:text="Wechat login -->" /> <Button android:id="@+id/button2" android:layout_width="240dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginStart="85dp" android:layout_marginEnd="86dp" android:layout_marginBottom="358dp" android:text="Mobile authentication code login -->" /> <Button android:id="@+id/button0" android:layout_width="237dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginStart="84dp" android:layout_marginEnd="89dp" android:layout_marginBottom="498dp" android:text="QQ Sign in -->" /> </RelativeLayout>
2. The corresponding activity is registered in the AndroidManifest.xml file
3. Layout effect:
2, How to realize jump -- the use of intent
1. Display the use of intent
1.1 set the current context environment and target Activity through explicit Intent to realize the first page Jump
eg: click QQ login on the home page to jump to the QQ information login page
① . layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:src="@drawable/qq" /> <FrameLayout android:layout_width="350dp" android:layout_height="70dp" android:layout_marginTop="50dp" > <EditText android:id="@+id/qq_num" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="QQ number/cell-phone number/mailbox" android:inputType="number" android:maxLines="1" android:textSize="20sp" android:gravity="center"/> <ImageView android:id="@+id/iv_et_num_delete" android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="right|center_vertical" android:layout_marginRight="20dp" android:background="@drawable/delete" /> </FrameLayout> <FrameLayout android:layout_width="350dp" android:layout_height="70dp" android:layout_marginTop="20dp" > <EditText android:id="@+id/qq_pwd" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="Input password" android:inputType="textPassword" android:maxLines="1" android:textSize="20sp" android:gravity="center"/> </FrameLayout> <ImageView android:id="@+id/qq_login" android:layout_width="102dp" android:layout_height="35dp" android:layout_marginTop="80dp" android:src="@drawable/go_right" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="60dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/qq_forgetpwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Forget password" android:textColor="#141313" /> <View android:layout_width="1dp" android:layout_height="10dp" android:layout_marginLeft="50dp" android:background="#141313" /> <TextView android:id="@+id/qq_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="User registration" android:textColor="#141313" /> </LinearLayout> </LinearLayout>
Envisaged effect:
② Activity file corresponding to. QQ login page
package com.example.test_intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class intent0 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.qq_login_test); } }
③ . intent usage
package com.example.test_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; public class SecondActivity extends Activity { //Association interface //override method @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_second_activity); //button0: the first page Jump is realized by explicitly setting the current context environment and target Activity with Intent. //button0 instantiation Button button00 = (Button) findViewById(R.id.button0); //Monitor button button00.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Display intent Intent intent0 = new Intent(SecondActivity.this,intent0.class);//Current context; Target class //Start activity startActivity(intent0); } }); }
④ 1. Registration information
<activity android:name=".intent0"> </activity>
1.2 the second page Jump is realized by explicitly setting the package name and the full path of the class where the target Activity is located through Intent
eg: click the second button to make the page Jump to the wechat information login page
① . wechat login page layout file
<?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" android:layout_height="match_parent" android:layout_width="match_parent"> <ImageView android:id="@+id/imageView4" android:layout_width="136dp" android:layout_height="126dp" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginTop="16dp" android:layout_marginEnd="12dp" app:srcCompat="@drawable/truth" /> <TextView android:id="@+id/textView2" android:layout_width="186dp" android:layout_height="48dp" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginStart="3dp" android:layout_marginEnd="164dp" android:layout_marginBottom="440dp" android:text="Wechat account has been associated, please enter the password" /> <EditText android:id="@+id/editTextTextPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="277dp" android:ems="10" android:gravity="center" android:hint="Please enter wechat password" android:inputType="textPassword" /> <TextView android:id="@+id/wechat_id" android:layout_width="176dp" android:layout_height="97dp" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginTop="23dp" android:layout_marginEnd="198dp" android:layout_marginBottom="611dp" android:ems="10" android:gravity="center" android:text="truth" android:textColor="#141313" android:textSize="30sp" android:textStyle="bold" /> <TextView android:id="@+id/qq_forgetpwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_marginStart="32dp" android:layout_marginBottom="213dp" android:text="Forget password" android:textColor="#141313" /> <View android:layout_width="3dp" android:layout_height="12dp" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginLeft="50dp" android:layout_marginEnd="178dp" android:layout_marginBottom="217dp" android:background="#141313" /> <TextView android:id="@+id/qq_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginLeft="50dp" android:layout_marginEnd="42dp" android:layout_marginBottom="212dp" android:text="User registration" android:textColor="#141313" /> <ImageView android:id="@+id/qq_login" android:layout_width="102dp" android:layout_height="35dp" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:layout_marginEnd="127dp" android:layout_marginBottom="122dp" android:src="@drawable/go_right" /> </RelativeLayout>
Design:
② . corresponding activity file
package com.example.test_intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class intent1Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wechat_login_test); } }
③ . intent method implementation
package com.example.test_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; public class SecondActivity extends Activity { //Association interface //override method @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_second_activity); //button1: the second page Jump is realized by explicitly setting the package name and the full path of the class where the target Activity is located through Intent. Button button10 = (Button) findViewById(R.id.button1); button10.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent1 = new Intent(); intent1.setClassName("com.example.test_intent", "com.example.test_intent.intent1Activity");//Package name and class name of the target parameter startActivity(intent1); } }); }
④ 1. Registration documents
<activity android:name=".intent1Activity"> </activity>
2. Implicit intent method
The third page Jump is realized by implicit Intent setting setAction()
eg: click mobile login to realize page Jump
① . layout file
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:name="action2"> <EditText android:id="@+id/editTextPhone" android:layout_width="237dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginStart="99dp" android:layout_marginTop="288dp" android:layout_marginBottom="398dp" android:ems="10" android:hint="Please enter your mobile number" android:gravity="center" android:inputType="phone" android:visibility="visible"/> <EditText android:id="@+id/editTextNumber" android:layout_width="131dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginEnd="184dp" android:layout_marginBottom="318dp" android:ems="10" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginEnd="71dp" android:layout_marginBottom="319dp" android:text="Verification Code" /> <ImageView android:id="@+id/qq_login" android:layout_width="102dp" android:layout_height="35dp" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginTop="80dp" android:layout_marginEnd="143dp" android:layout_marginBottom="241dp" android:src="@drawable/go_right" /> <TextView android:id="@+id/qq_forgetpwd" android:layout_width="62dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginEnd="253dp" android:layout_marginBottom="181dp" android:text="return" android:textColor="#141313" /> <View android:layout_width="1dp" android:layout_height="21dp" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginLeft="50dp" android:layout_marginEnd="192dp" android:layout_marginBottom="177dp" android:background="#141313" /> <TextView android:id="@+id/qq_register" android:layout_width="59dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginLeft="50dp" android:layout_marginEnd="72dp" android:layout_marginBottom="180dp" android:text="help" android:textColor="#141313" /> </RelativeLayout>
Design:
② . activity file
package com.example.test_intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class intent2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
③ . implementation method of implicit intent
Insert here package com.example.test_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; public class SecondActivity extends Activity { //Association interface //override method @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_second_activity); //button2: set setAction() through implicit Intent to realize the third page Jump. Button button20 = (Button) findViewById(R.id.button2); button20.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent2 = new Intent(); intent2.setAction("Action0"); startActivity(intent2); } }); }
④ 1. Registration information
<activity android:name=".intent2Activity"> <intent-filter> <action android:name="Action0" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
3, Overall structure
4, Effect achieved:
5, Problems and summary
1. The layout Design is inconsistent with the final implementation effect. When running the app, the designed page is not fully displayed
At present, it is thought that it may be related to the layout tag. It will be updated in time after it is solved
2. At the beginning of running the app, the program does not report an error and can open normally, but when you click the button, you will be prompted that "the application has been forcibly stopped", and then exit
This is because the code in the page is incomplete and the corresponding activity is not registered in the AndroidManifest.xml file, so the app file is incomplete. If you want to run normally, you need to check and ensure that each activity file has registration information