作为一个Android端的应用,多个Activity当然是避免不了的,我们该如何在多个Activity中跳转呢,这里就用到了Intent
下面我用一个按钮的响应跳转为例
先新建一个工程,这里我命名为IntentTest
在布局文件main里添加一个Button控件
代码如下
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
然后在主程序上设置按键的触发
完整代码为
MainActivity.java
package com.example.intenttest;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText("启动开关");
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//生成一Intent对象
Intent intent = new Intent();
intent.setClass(MainActivity.this,otherActivity.class);
MainActivity.this.startActivity(intent);
}
}
}
package com.example.intenttest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class otherActivity extends Activity{
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}
}
对otherActivity的布局设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
当编写一个新的Activity 需在AndroidManifest中进行注册:
位置在</activity>和</application>之间
代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".otherActivity" android:label="@string/other"/>
</application><pre name="code" class="html">
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentTest</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="other">otherActivity</string>
</resources>
结果