import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
主类
*/
private EditText et_name, et_pwd;
private Button jump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// 通过id找对象
et_name = (EditText) super.findViewById(R.id.et_name);
et_pwd = (EditText) super.findViewById(R.id.et_pwd);
jump = (Button) super.findViewById(R.id.jump);
jump.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 判断输入的用户名和密码是否一致
if (et_name.getText().toString().equals("admin")
&& et_pwd.getText().toString().equals("123")) {
Toast.makeText(MainActivity.this, "输入成功",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,
TiaoActivity.class);
intent.putExtra("name", et_name.getText().toString());
intent.putExtra("pwd", et_pwd.getText().toString());
// 开启意图
startActivityForResult(intent, 1);
} else {
Toast.makeText(MainActivity.this, "输入错误,请重新输入",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String xiu_name = data.getStringExtra("xiu_name");
String xiu_pwd = data.getStringExtra("xiu_pwd");
et_name.setText(xiu_name);
et_pwd.setText(xiu_pwd);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("activity_bundle", "测试数据");
Log.e("TAG", "onSaveInstanceState.........");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("TAG", "onDestroy.........");
}
@Override
protected void onPause() {
// 持久化操作
super.onPause();
Log.e("TAG", "onPause.........");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("TAG", "onRestart.........");
}
@SuppressLint("NewApi")
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
Log.e("TAG", "onRestoreInstanceState.........");
Log.e("TAG",
"onRestoreInstanceState----->"
+ savedInstanceState.getString("activity_bundle",
"erroy"));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("TAG", "onResume.........");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("TAG", "onStart.........");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("TAG", "onStop..........");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
跳转的目的Activity
package com.example.activitytest;
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.Toast;
public class TiaoActivity extends Activity {
private EditText et_name, et_pwd;
private Button back;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.back);
et_name = (EditText) super.findViewById(R.id.b_et_name);
et_pwd = (EditText) super.findViewById(R.id.b_et_pwd);
back = (Button) super.findViewById(R.id.back);
// 接收传来的意图
intent = getIntent();
String name1 = intent.getStringExtra("name");
String pwd1 = intent.getStringExtra("pwd");
et_name.setText(name1);
et_pwd.setText(pwd1);
// 给返回按钮加监听
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent inte = new Intent();
inte.setClass(TiaoActivity.this, MainActivity.class);
// 对传来的数据进行修改
inte.putExtra("xiu_name", et_name.getText().toString());
inte.putExtra("xiu_pwd", et_pwd.getText().toString());
Toast.makeText(TiaoActivity.this, "数据修改成功", Toast.LENGTH_SHORT)
.show();
setResult(2, inte);
finish();
}
});
}
}