初学者学习Activity之间的相互跳转
首先创建一个Activity
package com.example.acyaction;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button my_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_button = (Button) findViewById(R.id.my_button);
my_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent();
// intent.setClass(MainActivity.this, TestAcy.class);
// startActivity(intent);
// setTitle("other Activity");
Uri uri = Uri.parse("http://www.baidu.com"); // 指定网址
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW); // 指定Action
intent.setData(uri); // 设置Uri
MainActivity.this.startActivity(intent);
// startActivity(new Intent(MainActivity.this, TestAcy.class));
}
});
}
@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;
}
这是布局文件
}
<?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/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my button" />
</LinearLayout>
这是另一个需要跳转的Activity
package com.example.acyaction;
import android.app.Activity;
import android.os.Bundle;
public class TestAcy extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("other Activity");
setContentView(R.layout.test_activity);
}
}
<?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" >
<TextView
android:id="@+id/Text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个新的Activity" />
</LinearLayout>
别忘了修改配置文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/Activity跳转"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.acyaction.MainActivity"
android:label="@string/Activity跳转" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.acyaction.TestAcy"></activity>
</application>