textView1=(EditText)findViewById(R.id.editText1);
textView2=(EditText)findViewById(R.id.editText2);
textView3=(EditText)findViewById(R.id.editText3);
final Button btn1=(Button)findViewById(R.id.btn_start);
final Button btn2=(Button)findViewById(R.id.btn_close);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Second2Activity.class);
startActivityForResult(intent, SUBACTIVITY1);
}
});
btn1则是前面定义的按键绑定,可以更改,可以改成btn2.xxxxx
在主Activity定义2个标识号:
private static final int SUBACTIVITY1 =1;
private static final int SUBACTIVITY2 =2;
跳转到B界面使用该函数:
startActivityForResult(intent, SUBACTIVITY1);
将我们的这个信号传过去。跳转不同的界面,用不同的标识号,可以在前面定义。
之后设置回调函数:
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, requestCode, data);
switch (requestCode){
case SUBACTIVITY1:
if (resultCode == RESULT_OK) {
textView1.setText(data.getStringExtra("chejian"));
textView2.setText(data.getStringExtra("cangku"));
textView3.setText(data.getStringExtra("loudao"));
}
break;
case SUBACTIVITY2:
break;
default:
break;
}
}
在B界面调用setResult方法之后,A界面就已经收到了发送的数据
String uriString = editText.getText().toString();
String uriString2 = editText2.getText().toString();
String uriString3 = editText3.getText().toString();
Intent intent =new Intent(Second2Activity.this,MainActivity.class);
Bundle bundle = new Bundle();//创建并实例化一个Bundle对象
bundle.putCharSequence("chejian",uriString );
bundle.putCharSequence("cangku", uriString2);
bundle.putCharSequence("loudao", uriString3);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
则A界面 收到了信号RESULT_OK,
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, requestCode, data);
switch (requestCode){
case SUBACTIVITY1:
if (resultCode == RESULT_OK) {
textView1.setText(data.getStringExtra("chejian"));
textView2.setText(data.getStringExtra("cangku"));
textView3.setText(data.getStringExtra("loudao"));
}
break;
case SUBACTIVITY2:
break;
default:
break;
}
}
基于Android studio的车间通风数据采集系统实例视
MainActivity:
package com.huangfushi.hfs192025214_tongfengxitongcaiji;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int SUBACTIVITY1 =1;
private static final int SUBACTIVITY2 =2;
EditText textView1;
EditText textView2;
EditText textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(EditText)findViewById(R.id.editText1);
textView2=(EditText)findViewById(R.id.editText2);
textView3=(EditText)findViewById(R.id.editText3);
final Button btn1=(Button)findViewById(R.id.btn_start);
final Button btn2=(Button)findViewById(R.id.btn_close);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Second2Activity.class);
startActivityForResult(intent, SUBACTIVITY1);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, requestCode, data);
switch (requestCode){
case SUBACTIVITY1:
if (resultCode == RESULT_OK) {
textView1.setText(data.getStringExtra("chejian"));
textView2.setText(data.getStringExtra("cangku"));
textView3.setText(data.getStringExtra("loudao"));
}
break;
case SUBACTIVITY2:
break;
default:
break;
}
}
}
Second2:
package com.huangfushi.hfs192025214_tongfengxitongcaiji;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Second2Activity extends AppCompatActivity {
final public int CODE = 0x717;
private static final int SUBACTIVITY1 = 1;
private static final int SUBACTIVITY2 = 2;
private static final int SUBACTIVITY3 = 3;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
final EditText editText = (EditText) findViewById(R.id.edit);
final EditText editText2 = (EditText) findViewById(R.id.edit2);
final EditText editText3 = (EditText) findViewById(R.id.edit3);
final Button btnOK = (Button) findViewById(R.id.ok);
final Button btnCancel = (Button) findViewById(R.id.cancel);
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uriString = editText.getText().toString();
String uriString2 = editText2.getText().toString();
String uriString3 = editText3.getText().toString();
Intent intent =new Intent(Second2Activity.this,MainActivity.class);
Bundle bundle = new Bundle();//创建并实例化一个Bundle对象
bundle.putCharSequence("chejian",uriString );
bundle.putCharSequence("cangku", uriString2);
bundle.putCharSequence("loudao", uriString3);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED, null);
finish();
}
});
}
}
main.xml:
content_main.xml: