Android中提供一个叫Intent的类来实现屏幕之间的跳转,下面是一个简单的示例:
在应用中增加一个Activity(名字为.ForwardTarget),这需要修改AndroidManifest.xml文件,如下:
Xml代码
<?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>
然后在layout中的main加入一个id为leah1的按钮,另外再创建一个任意的layout(将要跳转到得layout),我取名为leah1。
接下来是两个类,一个是AndroidForward,另一个是将要跳转到得ForwardTarget,代码分别如下:
AndroidForw:
Java代码
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:
Java代码
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);
}
}
没有评论:
发表评论