andorid_activity简单的入门

本文将指导您如何在Android应用程序中创建并跳转至多个界面,通过定义布局、继承Activity类并设置onCreate方法来实现界面显示。此外,文章还介绍了如何在AndroidManifest.xml中注册Activity,并使用Intent进行界面间的跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activity (组件)

一个界面对应一个activity

如何在应用程序里面创建多个界面

1. 声明一个布局
2. 定义一个类, 继承activity
    在activity里面的onCreate方法指定显示的布局  setContentView(R.layout.xxx);
3. 登记注册activity

入门小示例
需求:从点击按钮从一个activity跳转到另外一个activity

第一步:布局
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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:onClick="showOther"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:text="点击跳转" />

</RelativeLayout>


activity_other.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第二个界面" />

</RelativeLayout>
  1. 定义一个类, 继承activity
    在activity里面的onCreate方法指定显示的布局 setContentView(R.layout.xxx);

MainActivity的代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置了该界面,显示什么布局 (显示什么内容)
        setContentView(R.layout.activity_main);
    }

    public void showOther(View v){
    //这个startAvtivity(Intent intent)是跳转页面的核心方法。调用该方法,
    //就会跳转到,意图对象,指定的类   
    //new Intent(this , OtherActivity.class)第一个参数是上下文,第二个是指定的类
   //如果是跳转到Activity,那么这个类必须要继承Activity
    startActivity(new Intent(this , OtherActivity.class));

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

OtherActivity的代码

public class OtherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
    }
}

AndroidManifest.xml清单文件

        //这也是注册activity,这个的是activity工程建立好就有的了
         <activity           
            android:name="com.itheima.mulitui.MainActivity"
            android:label="@string/app_name" >
            //这是意图过滤器,这有满足了意图过滤器activity,才能跳转过来,
            //一般用于隐式 跳转
            <intent-filter>
               // 动作 : 这里声明了应用程序的入口界面
                <action android:name="android.intent.action.MAIN" /> 
                //分类: 指定了再应用程序列表里面有一个图标
                 <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        //我们自己另外添加的otherActivity,登记注册Activity
         <activity
            android:name="com.itheima.mulitui.OtherActivity"
            android:label="02多界面应用"           
            >

        </activity>

意图过滤器的小说明

 <intent-filter>
                动作 : 这里声明了应用程序的入口界面
                <action android:name="android.intent.action.MAIN" />

                分类: 指定了再应用程序列表里面有一个图标
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值