登录
转载

【Android】activity入门

发布于 2021-03-18 阅读 421
  • Android
转载

Activity 页面】
一. 自定义Activity
项目的创建的时候会自动建一个主页面(MainActivity),自己写一个页面替换主页面(MainActivity)。

步骤:
1.写一个布局.xml
2.写一个.java,继承Activity类并导包
3.在清单文件中注册

Tip:图片命名必须是小写英文+下划线。

二. Activity跳转
1.创建Intent对象,并导包
2.给构造方法传入参数,参数一:当前的页面的名称.this;参数二:要跳转到的页面的名称.class
3.调用startActivity方法传入intent对象作为参数

实例
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);
    }

}



com/example/test/MyActivity.java
实现点击图片跳转至MainActivity


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 OnClickListener() {

            @Override
            public void onClick(View v) {
                // 点击控件要做的事情
                // 从MyActivity.this → MainActivity.class
                Intent intent = new Intent(MyActivity.this,MainActivity.class);
                // 调用方法传入参数进行跳转
                startActivity(intent);
            }
        });
    }




}


src/main/AndroidManifest.xml
设置主页面为自定义的MyActivity,并注册该页面

<?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
            android:name="com.example.test.MyActivity"
            android:label="自定义页面" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


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>



layout/activity_my.xml

<?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>


image.png

点击图片跳转

image.png

三. Activity传值
使用Intent对象来携带数据进行跳转传值。

实例
com/example/test/MyActivity.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 OnClickListener() {

            @Override
            public void onClick(View v) {
                // 从MyActivity.this → MainActivity.class
                Intent intent = new Intent(MyActivity.this,MainActivity.class);
                //携带数据跳转传值
                //参数一:数据的名称(键)
                //参数二:数据的值(值)
                intent.putExtra("data", "这是个随便写的数据仅此而已,也支持其他类型");
                startActivity(intent);
            }
        });
    }




}



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 intent = getIntent();
        // 再取出其中的内容,参数是数据的名称(键),返回值是数据的值(值)
        String message = intent.getStringExtra("data");
        // 如果是其他数据类型
//        intent.getIntExtra(name, defaultValue)
//        intent.getDoubleExtra(name, defaultValue)

        // 给上面的文字显示控件对象设置显示的内容
        mText.setText(message);

    }

}




src/main/AndroidManifest.xml
设置主页面为自定义的MyActivity,并注册该页面

<?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
            android:name="com.example.test.MyActivity"
            android:label="自定义页面" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


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>



<?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>


image.png

点击图片跳转

image.png

————————————————
版权声明:本文为CSDN博主「SuperSources」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45654306/article/details/108012901

评论区

莲子
4粉丝

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报