1. Realize the Android interface, and realize the jump through the intent, the interface displays the student's name, student ID, email.
2. The realization of the intent is required to pass the name, student ID, email and other data to the second activity;
3. At the same time The results can be run on virtual machines and mobile phones;
4. Use preferences to realize the storage, storage and reading of the above data.
5. Learn how to set breakpoints, and learn how to debug programs in debug mode.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是MainActivity"
android:layout_marginTop="20px"
android:layout_centerHorizontal="true"
android:id="@+id/txt" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:layout_centerHorizontal="true"
android:id="@+id/edit1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit1"
android:layout_centerHorizontal="true"
android:id="@+id/edit2"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit2"
android:layout_centerHorizontal="true"
android:id="@+id/edit3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit3"
android:layout_centerHorizontal="true"
android:text="输入文本,跳转到下一个页面"
android:id="@+id/btn"/>
</RelativeLayout>
activity_another.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到第二个页面"
android:layout_marginTop="20px"
android:layout_centerHorizontal="true"
android:id="@+id/txt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt11"
android:layout_centerHorizontal="true"
android:layout_below="@id/txt"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt22"
android:layout_centerHorizontal="true"
android:layout_below="@id/txt11"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt33"
android:layout_centerHorizontal="true"
android:layout_below="@id/txt22"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt33"
android:layout_centerHorizontal="true"
android:text="返回第一个页面"
android:id="@+id/btn2"/>
</RelativeLayout>
MainActivity
package com.example.myapplication1;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn;
EditText edit1,edit2,edit3;
String s1,s2,s3;
SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences = getSharedPreferences("userinfo",MODE_PRIVATE);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
edit3 = (EditText)findViewById(R.id.edit3);
btn = (Button)findViewById(R.id.btn);
String id = sharedPreferences.getString("id","");
String name = sharedPreferences.getString("name","");
String email = sharedPreferences.getString("email","");
edit1.setText(id);
edit2.setText(name);
edit3.setText(email);
mEditor = sharedPreferences.edit();
btn.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
s1 = edit1.getText().toString();
s2 = edit2.getText().toString();
s3 = edit3.getText().toString();
mEditor.putString("id",s1);
mEditor.putString("name",s2);
mEditor.putString("email",s3);
mEditor.commit();
Bundle bundle = new Bundle();
bundle.putString("edit1",s1);
bundle.putString("edit2",s2);
bundle.putString("edit3",s3);
intent.putExtras(bundle);
startActivity(intent);
}
}
Main2Activity
package com.example.myapplication1;
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;
import static com.example.myapplication1.R.layout.activity_another;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
Button btn2;
TextView txt11,txt22,txt33;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_another);
txt11 = (TextView)findViewById(R.id.txt11);
txt22 = (TextView)findViewById(R.id.txt22);
txt33 = (TextView)findViewById(R.id.txt33);
btn2 = (Button)findViewById((R.id.btn2));
Bundle bb = this.getIntent().getExtras();
String str1 = bb.getString("edit1");
String str2 = bb.getString("edit2");
String str3 = bb.getString("edit3");
txt11.setText(str1);
txt22.setText(str2);
txt33.setText(str3);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent2 = new Intent();
intent2.setClass(this,MainActivity.class);
startActivityForResult(intent2,0);
}
}