不同的2个程序彼此调用

不同的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 Activity implements

       OnClickListener {

    private Button btn = null;

    private TextView tv = 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) {

           case RESULT_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 Button btn = null;

    private TextView tv = 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();

           }

       });

    }

 

}

 

布局文件:

<?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/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>

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值