初学安卓 - 打开另一个activity

本文通过一个简单的示例展示了如何在Android应用中实现不同Activity之间的通信。从MainActivity启动ShowActivity,并传递了一条消息,该消息在新的Activity中显示出来。

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

MainActivity:

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

    <Button 
        android:id="@+id/btnSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OPEN ANOTHER"
        android:textColor="@color/red"
        android:onClick="openAnother"/>

</RelativeLayout>


java

package com.zsh.mk1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

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

	@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;
	}
	
	public void openAnother(View view){
		Intent intent = new Intent(this, ShowActivity.class);
		intent.putExtra("msg", "新开一个Activity");
		startActivity(intent);
	}

}



com.zsh.mk1.ShowActivity:

package com.zsh.mk1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ShowActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		
		TextView tv = new TextView(this);
		tv.setText(intent.getStringExtra("msg"));
		
		setContentView(tv);
	}
	
}



AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zsh.mk1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zsh.mk1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- 一定要加,否则程序会停止运行 -->
        <activity
            android:name="com.zsh.mk1.ShowActivity"
            android:parentActivityName="com.zsh.mk1.MainActivity"/>
    </application>

</manifest>


总结:

虽然是很简单的一个调用,但练手的时候还是触礁了~

1、忘记在manifest里添加activity的配置;

2、用代码写了控件之后忘记setContentView;

3、颜色代码,也总是忘记在res/values/下面的xml里面添加(总习惯直接用“red”之类的)。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值