1.Intent实例对象链接当前页面和目标页面,对象中可以存放键值对的数据,通过startActivity或者startActivityForResult启动新页面,原始页面操作如下:
Intent it = new Intent(MainActivity.this, CheckUserInfo.class); it.putExtra("userName", MainActivity.this.userNameMain.getText().toString()); MainActivity.this.startActivity(it);在新页面中采用如下代码即可获得传过来的值:
Intent it = super.getIntent(); String txtUserName = it.getStringExtra("userName");2.回传数据
Intent it = new Intent(MainActivity.this, CheckUserInfo.class); it.putExtra("userName", MainActivity.this.userNameMain.getText().toString()); MainActivity.this.startActivityForResult(it, 1);在新页面中除了需要传值还需要设置回传结果并关闭当前页面,代码如下:
CheckUserInfo.this.getIntent().putExtra("userInfo", "一些数据"); CheckUserInfo.this.setResult(RESULT_OK, CheckUserInfo.this.getIntent()); CheckUserInfo.this.finish();3.在AndroidManifest.xml配置新加的页面,在Application节点下加上如下代码:
<activity android:name="CheckUserInfo" android:label="@string/app_name"/>项目完整代码:
设计页面activity_main.xml:
<?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="您的姓名是:" /> <EditText android:id="@+id/userNameMain" android:layout_width="180dp" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btnCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证" /> <TextView android:id="@+id/userInfoMain" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>设计页面checkuser.xml:
<?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/userName" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入绑定帐号:" /> <EditText android:id="@+id/accountName" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入帐号密码:" /> <EditText android:id="@+id/accountPwd" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btnCheckReturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证返回" /> <Button android:id="@+id/btnCancelReturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消验证" /> </LinearLayout>后台逻辑MainActivity.java:
package com.example.simpleintent0815; 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; import android.widget.TextView; public class MainActivity extends Activity { private Button btnCheck = null; private EditText userNameMain = null; private TextView userInfoMain = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.btnCheck = (Button) super.findViewById(R.id.btnCheck); this.userNameMain = (EditText) super.findViewById(R.id.userNameMain); this.userInfoMain = (TextView) super.findViewById(R.id.userInfoMain); this.btnCheck.setOnClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { Intent it = new Intent(MainActivity.this, CheckUserInfo.class); it.putExtra("userName", MainActivity.this.userNameMain.getText() .toString()); // MainActivity.this.startActivity(it); MainActivity.this.startActivityForResult(it, 1); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case RESULT_OK: MainActivity.this.userInfoMain.setText(data .getStringExtra("userInfo")); break; case RESULT_CANCELED: MainActivity.this.userInfoMain.setText("您已取消验证!"); break; default: break; } } }后台逻辑CheckUserInfo.java:
package com.example.simpleintent0815; 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; import android.widget.TextView; public class CheckUserInfo extends Activity { private TextView userName = null; private Button btnCheckReturn = null; private Button btnCancelReturn = null; private EditText accountName = null; private EditText accountPwd = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.checkuser); this.userName = (TextView) super.findViewById(R.id.userName); this.btnCheckReturn = (Button) super.findViewById(R.id.btnCheckReturn); this.btnCancelReturn = (Button) super .findViewById(R.id.btnCancelReturn); this.accountName = (EditText) super.findViewById(R.id.accountName); this.accountPwd = (EditText) super.findViewById(R.id.accountPwd); Intent it = super.getIntent(); String txtUserName = it.getStringExtra("userName"); this.userName.setText("您的姓名是:" + txtUserName); this.btnCheckReturn.setOnClickListener(new OnClickListenerImpl()); this.btnCancelReturn .setOnClickListener(new OnClickListenerImplCancel()); } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { String userInfo = "您输入的验证信息是:“账户名:" + CheckUserInfo.this.accountName.getText().toString() + ";密码:" + CheckUserInfo.this.accountPwd.getText().toString() + ";”"; CheckUserInfo.this.getIntent().putExtra("userInfo", userInfo); CheckUserInfo.this.setResult(RESULT_OK, CheckUserInfo.this.getIntent()); CheckUserInfo.this.finish(); } } private class OnClickListenerImplCancel implements OnClickListener { @Override public void onClick(View v) { CheckUserInfo.this.setResult(RESULT_CANCELED, CheckUserInfo.this.getIntent()); CheckUserInfo.this.finish(); } } }项目运行截图:
后记:写代码时由于基本语法薄弱,遇到一坑爹的问题,在传值时获取文本信息没写toString导致一直传不过去,耗时两个多小时才找到问题,错误元凶MainActivity.this.userNameMain.getText().toString();希望不要犯同样的错误。
项目源码下载