一个调用其他activity的完整例子,使用了 onActivityResult和startActivityForResult

https://blog.csdn.net/qq_32521313/article/details/52451364

 

Android startActivityForResult基本用法
2016年09月06日 16:46:07
阅读数:17007
startActivityForResult和startActivity的区别是一个是带值传输,一个是直接跳转

具体用法如下:

activity_main:

[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="进入" />

</RelativeLayout>
activity_main1:
[html] view plain copy
<?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" >

<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入数字"

/>
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反回"

/>
</LinearLayout>
MainActivity:
[java] view plain copy
package com.example.demo1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
SecondActivity.class), 1);
}
});
textview=(TextView) findViewById(R.id.textview);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 4) {
String s=data.getStringExtra("bian");
textview.setText(s);
}
}

}
SecondActivity:
[java] view plain copy
package com.example.demo1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
final EditText text1=(EditText) findViewById(R.id.text1);
Button finish=(Button) findViewById(R.id.finish);
finish.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent i=new Intent();
i.putExtra("bian", text1.getText().toString());
setResult(4,i);
finish();
}
});
}
}

别忘了注册
[html] view plain copy
<activity android:name="com.example.demo1.SecondActivity"></activity>

posted @ 2018-05-22 13:26  strongdady  阅读(150)  评论(0编辑  收藏  举报