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”之类的)。