实现的业务是这样的,有两个界面,登陆界面和注册界面,在登录界面点击“注册”按钮,启动注册界面,用户注册成功后,将注册的账号和密码传回到登陆界面的账号密码文本框内。
具体操作如下:
public class MainActivity extends Activity { private EditText et_login_name; private EditText et_login_pwd; private Button btn_login_register; private Button btn_login_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setViews(); setListener(); } private void setListener() { btn_login_register.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, RegisterActivity.class); int requestCode = 100; startActivityForResult(intent, requestCode);//启动activity } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {//当从RegisterActivity发送数据时,会触发此事件 if(requestCode == 100 && resultCode == this.RESULT_OK){ String name = data.getStringExtra("name"); String pwd = data.getStringExtra("pwd"); et_login_name.setText(name); et_login_pwd.setText(pwd); } super.onActivityResult(requestCode, resultCode, data); } private void setViews() { et_login_name =(EditText) findViewById(R.id.et_login_name); et_login_pwd =(EditText) findViewById(R.id.et_login_pwd); btn_login_register = (Button)findViewById(R.id.btn_login_register); btn_login_login = (Button)findViewById(R.id.btn_login_login); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
public class RegisterActivity extends Activity { private EditText et_register_name; private EditText et_register_pwd; private Button btn_register_register; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); setViews(); setListener(); } private void setViews() { et_register_name = (EditText)findViewById(R.id.et_register_name); et_register_pwd = (EditText)findViewById(R.id.et_register_pwd); btn_register_register = (Button)findViewById(R.id.btn_register_register); } private void setListener() { btn_register_register.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { try{ String name = et_register_name.getText().toString(); String pwd = et_register_pwd.getText().toString(); Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show(); Intent data = new Intent(); data.putExtra("name", name); data.putExtra("pwd", pwd); RegisterActivity.this.setResult(RegisterActivity.this.RESULT_OK, data);//向MainActivity传送数据 RegisterActivity.this.finish(); }catch (Exception e) { e.printStackTrace(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.register, menu); return true; } }
原文:http://www.cnblogs.com/fazheng/p/5212409.html