Android studio新建一个activity

1、

res->layout右键新建一个xml->layout xml file

 名字,布局

 然后随便写点东西到新建的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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三个页面"/>
</LinearLayout>

2、

app->java->第一个 。右键->新建->java类

 

 加入代码

import android.os.Bundle;

public class third extends MainActivity{ //MainActivity是第一个java类的名字
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);//注意为“自己新建的类名”
    }
}

3、

在app->manifest->AndroidManifest.xml文件中的<application>标签内添加一个新标签,注册这个activity

        <activity
            android:name=".third"   
            android:label="ActivityThird">
            <intent-filter>
                <action android:name="mythird"/>
                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </activity>

name是新建的activity名字,后面的label、action android:name随意,自己记得就好了

到这一步就已经建完了。配置一个按钮检测一下

在Activity_Main.xml中新建一个按钮,

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

android:onClick="onClick"

android:id="@+id/next3" android:text="NEXT"/>

在MainActivity.java中写onClick函数。不要写在onCreate函数里面了

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.next2:
                startActivity(new Intent("mySecond"));
            case R.id.next3:
                startActivity(new Intent("myThird"));
        }

    }

 这里的switch用来获取按钮id,用来区分是按下的哪个按钮。后面的这两个东西()就是之前在manifest文件中添加的<action android:name=“”>。编译器不会联想,要自己输入,区分大小写

 

原文地址:https://www.cnblogs.com/This-is-Y/p/14551779.html