mainactivity1
package com.example.android_test05;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
String name = username.getText().toString().trim();
String id = password.getText().toString().trim();
Bundle bundle = new Bundle();
bundle.putString("username",name);
bundle.putString("password",id);
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this,MainActivity2.class);
intent1.putExtras(bundle);
startActivity(intent1);
}
});
}
}
mainactivity2
package com.example.android_test05;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
private TextView Show1;
private TextView Show2;
private TextView Show3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Show1 = (TextView) findViewById(R.id.username);
Show2 = (TextView) findViewById(R.id.password);
Show3 = (TextView) findViewById(R.id.welcome);
//通过获取internet。得到附带的数据,赋值给number
Bundle bundle = this.getIntent().getExtras();
//通过bundle获取传递过来的字段
String name = bundle.getString("username");
String id = bundle.getString("password");
Show1.setText("账号:" + name);
Show2.setText("密码:" + id);
Show3.setText("欢迎" + name+"登录本系统!");
Button button2 = (Button) this.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setClass(MainActivity2.this, MainActivity.class);
startActivity(intent1);
}
});
}
}
xml1
xml2



