可借用代码部分:
1.绑定按钮与文本与当按下按键的操作:
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
2.A的界面跳转到B的界面执行相应的操作。操作执行完毕之后,从B的界面跳转回A的界面,并传输回一些数据(onActivityResult函数的用法)
在主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:
<?xml version="1.0" encoding="utf-8"?>
<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="com.huangfushi.hfs192025214_tongfengxitongcaiji.MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_header"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/app_name"
android:textSize="26sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearUp"
android:layout_below="@+id/text_header"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_marginLeft="10dp"
android:text="车间通风系统:"
android:textSize="20sp"
/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:editable="false"
android:inputType="none"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginLeft="10dp"
android:text="仓库通风数据:"
android:textSize="20sp"
/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:editable="false"
android:inputType="none"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_marginLeft="10dp"
android:text="楼道通风数据:"
android:textSize="20sp"
/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:editable="false"
android:inputType="none"
/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_start"
android:layout_marginLeft="10dp"
android:layout_below="@+id/linearUp"
android:padding="15dp"
android:textSize="20sp"
android:text="采集通风数据"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_close"
android:layout_toLeftOf="@+id/btn_start"
android:layout_below="@+id/linearUp"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:padding="15dp"
android:textSize="20sp"
android:text="关闭系统"
/>
</RelativeLayout>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textShow1"
android:text="输入车间数据"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:lines="1"
android:inputType="textUri" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textShow2"
android:text="输入仓库数据"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit2"
android:lines="1"
android:inputType="textUri" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textShow3"
android:text="输入楼道数据"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit3"
android:lines="1"
android:inputType="textUri" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ok"
android:text="接受"
android:padding="3dip"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:padding="3dip"
android:text="撤销"
/>
</LinearLayout>
|