不同的2个程序彼此调用
来源:互联网 发布:马克威算法学院 编辑:程序博客网 时间:2024/05/21 17:53
不同的2个程序彼此调用
新建一个继承Activity类的StrartAnotherAndForResultActivity,并设置布局文件为:startanotherandforresult.xml。
首先在布局文件中添加一个TextView和一个Button,TextView用于显示程序运行结果,Button用于调用另外一个程序。
<TextView
android:id="@+id/startanotherforresult_tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/strartanotherandforresultactivity"
android:textSize="18sp"/>
<Button
android:id="@+id/startanotherforresult_btn01"
style="@android:style/Widget.Button.Inset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startanotherproject"/>
界面效果:
而后在Activity程序获取组件,为Button添加单击事件。
package lyx.feng.simpletextdemo;
......
public class StartAnotherAndForResultActivity extends Activityimplements
OnClickListener {
private Buttonbtn = null;
private TextViewtv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.startanotherandforresult);
this.btn = (Button)super
.findViewById(R.id.startanotherforresult_btn01);
this.tv = (TextView)super
.findViewById(R.id.startanotherforresult_tv01);
this.btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
接着在onClick()方法中通过Intent调用一个程序。
Intent intent = new Intent();
try {
intent.setClassName("lyx.feng.helloworld","MainActivity");
Bundle bundle = new Bundle();
bundle.putString("info","这个SimpleTextDemo传递过来的数据");
intent.putExtras(bundle);
startActivityForResult(intent, 1);
} catch (Exception e) {
Toast.makeText(this,"你还没有安装HelloWorld这个程序!", Toast.LENGTH_SHORT)
.show();
}
接着覆写onActivityResult()方法
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
if (requestCode == 1) {
switch (resultCode) {
caseRESULT_OK:
tv.setText(data.getExtras().getString("back"));
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
接着贴上HelloWorld的Activity代码。
MainActivity:
package lyx.feng.helloworld;
......
public class MainActivity extends Activity {
private Buttonbtn = null;
private TextViewtv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.btn = (Button)super
.findViewById(R.id.startanotherforresult_btn01);
this.tv = (TextView)super
.findViewById(R.id.startanotherforresult_tv01);
Bundle bundle = getIntent().getExtras();
if (bundle !=null) {
this.tv.setText(bundle.getString("send"));
} else {
this.btn.setEnabled(false);
}
this.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("back","这个HelloWorld返回的数据");
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
布局文件:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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/startanotherforresult_tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/_helloworld"
android:textSize="18sp"/>
<Button
android:id="@+id/startanotherforresult_btn01"
style="@android:style/Widget.Button.Inset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/_"/>
</LinearLayout>
- 不同的2个程序彼此调用
- android之两个不同程序的彼此调用
- 2个android程序之间的调用
- 给定两个整形,求彼此二进制的不同位数
- office中onenote如何将页面内的2个词彼此链接
- C++两个类互相调用彼此的方法
- C++两个类互相调用彼此的方法
- 调用小程序查看器的2个方法
- C++程序中不同的函数调用方式
- 自定义权限实现不同程序间的调用
- ios程序不同状态下,调用的代理方法
- Linux上的C程序不同文件调用
- 彼此的幸福--陌路
- 彼此的约定
- 一个通过不同的文件扩展名来调用不同的解压程序的shell脚本
- 经验总结-1-不同类之间调用彼此方法的几种情况
- C++ 两个包含类互相调用彼此的类成员变量和方法
- 不同包的调用
- 仿易迅登录界面
- JLTrackLabelView
- poj 3254Corn Fields(状态压缩dp)
- OpenCL函数-1
- [c++]继承的经典例子
- 不同的2个程序彼此调用
- 用Qt实现一个桌面弹幕程序(七)-- -- 桌面客户端实现③
- Java反射-精简小结
- 关于js和java的一点点不同
- 【bzoj3926】[Zjoi20150]诸神眷顾的幻想乡 后缀自动机+trie
- 关于javascript的类的详解
- java的if else if语句的判断条件不能相同
- TabPageIndicator 分割线
- 1>XxxDlg.obj : error LNK2005: "char * * tabPageData" (?tabPageData@@3PAPADA) 已经在 XxxDlg.obj 中定义