|
我们曾经在一篇文章中为大家详细介绍过有关Android Activity的相关内容。在这里,我们会进一步的对这一操作进行详细剖析。主要就是针对Android Activity跳转的实现。下面就让我们一起看看具体操作方法吧。
Android中提供一个叫Intent的类来实现屏幕之间的跳转,下面是一个简单的示例:
在应用中增加一个Activity(名字为.ForwardTarget),这需要修改AndroidManifest.xml文件,如下:
Android Activity跳转代码示例:
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="< A href="http://schemas.android.com/
apk/res/android">http://schemas.android.com/apk/res/android< /A>"
package="com.ray.forward"
android:versionCode="1"
android:versionName="1.0">
< application android:icon="@drawable/icon"
android:label="@string/app_name">
< activity android:name=".androidForward"
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=".ForwardTarget">
< /activity>
< /application>
< uses-sdk android:minSdkVersion="3" />
< /manifest>
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.ray.forward"
android:versionCode="1"
android:versionName="1.0">
< application android:icon="@drawable/icon"
android:label="@string/app_name">
< activity android:name=".androidForward"
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=".ForwardTarget">
< /activity>
< /application>
< uses-sdk android:minSdkVersion="3" />
< /manifest>
复制代码
然后在layout中的main加入一个id为leah1的按钮,另外再创建一个任意的layout(将要跳转到得layout),我取名为leah1。
接下来是两个类,一个是AndroidForward,另一个是将要跳转到得ForwardTarget,Android Activity跳转实现的代码分别如下:
AndroidForw:
package com.ray.forward;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class androidForward extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.leah1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(androidForward.this, ForwardTarget.class);
startActivity(intent);
finish();
//停止当前的Activity,如果不写,则按返回键会跳转回原来的Activity
}
});
}
}
package com.ray.forward;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class androidForward extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.leah1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(androidForward.this, ForwardTarget.class);
startActivity(intent);
finish();
//停止当前的Activity,如果不写,则按返回键会跳转回原来的Activity
}
});
}
}
ForwardTarget:
package com.ray.forward;
import android.app.Activity;
import android.os.Bundle;
public class ForwardTarget extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.leah1);
}
}
复制代码 |
|