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);
Bundle bundle = this.getIntent().getExtras();
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
<?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="wrap_content"
android:layout_height="wrap_content"
android:translationX="100dp"
android:translationY="150dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="25dp"></TextView>
<EditText
android:id="@+id/username"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:translationX="100dp"
android:translationY="180dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25dp"></TextView>
<EditText
android:inputType="numberPassword"
android:id="@+id/password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入密码">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationX="0dp"
android:translationY="230dp">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="25dp"></Button>
</LinearLayout>
</LinearLayout>
xml2
<?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="wrap_content"
android:layout_height="wrap_content"
android:translationX="80dp"
android:translationY="100dp">
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="28dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:translationX="100dp"
android:translationY="150dp">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:translationX="100dp"
android:translationY="180dp">
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="250dp">
<Button
android:id="@+id/button2"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/back"
android:translationX="380dp"></Button>
</LinearLayout>
</LinearLayout>
|