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

 

 

 

 

 

 

0 0
原创粉丝点击