The Activity pages
A custom Activity project will automatically create a main page (MainActivity), write a page to replace the main page (MainActivity).
Step: 1. Write a layout. XML. 2. Register in the manifest file
The image name must be lowercase + underscore.
Create an Intent object and guide the packet. Pass the constructor an argument, one: the name of the current page. Class 3. Call the startActivity method, passing in an Intent object as an argument
Instance of the com/example/test/MainActivity. Java
package com.example.test; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}Copy the code
Com/example/test/MyActivity does. Click image jump to MainActivity Java implementation
package com.example.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class MyActivity extends Activity { private ImageView mImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the layout file setContentView(r.layout.activity_my) to use; mImage = (ImageView) findViewById(R.id.my_image); MImage. SetOnClickListener (new an OnClickListener () {@ Override public void onClick (View v) {/ / to do / / click control From the MyActivity does. This - > MainActivity. Class Intent Intent = new Intent (MyActivity does. This, MainActivity. Class); StartActivity (intent); }}); }}Copy the code
SRC/main/AndroidManifest. XML set the home page for custom MyActivity does, and register the page
<? The XML version = "1.0" encoding = "utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test"> <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=".MainActivity" android:label="@string/app_name" > </activity> <activity The android: name = "com. Example. The test. The MyActivity does" android: label = "custom page" > < intent - filter > < action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>Copy the code
layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@android:dimen/app_icon_size"
android:text="hello,this is the original activity"
android:textSize="100px"
/>
</RelativeLayout>
Copy the code
layout/activity_my.xml
<? The 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" android:orientation="vertical" > <ImageView android:id="@+id/my_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic" /> </LinearLayout>Copy the code
Click the picture to jump
An Intent object is used to carry data for jumping to and from an Activity.
Instance of the com/example/test/MyActivity does. Java
package com.example.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class MyActivity extends Activity { private ImageView mImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mImage = (ImageView) findViewById(R.id.my_image); MImage. SetOnClickListener (new an OnClickListener () {@ Override public void onClick (View v) {/ / from MyActivity does this - > MainActivity.class Intent intent = new Intent(MyActivity.this,MainActivity.class); // Intent. PutExtra ("data", "data"); // Intent. startActivity(intent); }}); }}Copy the code
com/example/test/MainActivity.java
package com.example.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView mText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mText = (TextView) findViewById(R.id.text1); Intent Intent = getIntent(); String message = intent.getStringExtra("data"); intent.getStringExtra("data"); // Intent.getinTextra (name, defaultValue) // Intent.getDoubleExtra (name, intent.getDoubleExtra) Mtext.settext (message); }}Copy the code
SRC/main/AndroidManifest. XML set the home page for custom MyActivity does, and register the page
<? The XML version = "1.0" encoding = "utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test"> <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=".MainActivity" android:label="@string/app_name" > </activity> <activity The android: name = "com. Example. The test. The MyActivity does" android: label = "custom page" > < intent - filter > < action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>Copy the code
layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@android:dimen/app_icon_size"
android:text="hello,this is the original activity"
android:textSize="100px"
/>
</RelativeLayout>
Copy the code
<? The 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" android:orientation="vertical" > <ImageView android:id="@+id/my_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic" /> </LinearLayout>Copy the code
Click the picture to jump
Copyright notice: This article is originally published BY CSDN blogger “SuperSources” under CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement. Original link: blog.csdn.net/qq_45654306…