下面举例添加一个WebViewIntentDemoActivity
第一步:先将java文件导入集合项目中
第二步:创建新的main界面,导入需要增加的Activity界面(记得与前面的命名区分开)
第三步:在集合Activity界面中添加一个新按钮
第四步:在集合的Activity中添加按钮的引用以及跳转功能
(新按钮的引用)
(获取新按钮的引用)
(添加点击跳转)
package com.example.activitycommunication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int SUBACTIVITY1 = 1;
private static final int SUBACTIVITY2 = 2;
TextView textView;
Button btnBroadcast; // 新按钮的引用
Button btnIntent;//添加新按钮的引用
Button btnWebViewIntent;//添加新按钮的引用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textShow);
final Button btn1 = (Button) findViewById(R.id.btn1);
final Button btn2 = (Button) findViewById(R.id.btn2);
btnBroadcast = (Button) findViewById(R.id.btnBroadcast); // 获取新按钮的引用
btnIntent = (Button) findViewById(R.id.btnIntent);// 获取新按钮的引用
btnWebViewIntent = (Button) findViewById(R.id.btnWebViewIntent);// 获取新按钮的引用
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SubActivity1.class);
startActivityForResult(intent, SUBACTIVITY1);
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SubActivity2.class);
startActivityForResult(intent, SUBACTIVITY2);
}
});
btnBroadcast.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, BroadcastReceiverDemoActivity.class);
startActivity(intent); // 启动 BroadcastReceiverDemoActivity
}
});
btnIntent.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, IntentDemoActivity.class);
startActivity(intent); // 启动 IntentDemoActivity
}
});
btnWebViewIntent.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, WebViewIntentDemoActivity.class);
startActivity(intent); // 启动 WebViewIntentDemoActivity
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SUBACTIVITY1:
if (resultCode == RESULT_OK) {
Uri uriData = data.getData();
textView.setText(uriData.toString());
}
break;
case SUBACTIVITY2:
break;
}
}
}
第五步:在xml中添加相应配置
然后就可以运行成功啦!