Activity界面基本,实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email.

1、实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email.
2、要求intent的实现传递姓名,学号,email等数据,到第二个activity;
3、同时要求可以在虚拟机及手机上运行结果;
4、采用preference实现对如上数据的存储,存储及读取。
5、学会如何设置断点,并学会debug模式下,调试程序。

activity_main.xml

"1.0" encoding="utf-8"?>
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">

    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" />

    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt"
        android:layout_centerHorizontal="true"
        android:id="@+id/edit1"/>
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit1"
        android:layout_centerHorizontal="true"
        android:id="@+id/edit2"/>
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit2"
        android:layout_centerHorizontal="true"
        android:id="@+id/edit3"/>
    

activity_another.xml

"1.0" encoding="utf-8"?>
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">

    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎来到第二个页面"
        android:layout_marginTop="20px"
        android:layout_centerHorizontal="true"
        android:id="@+id/txt" />

    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt11"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/txt"/>
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt22"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/txt11"/>
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt33"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/txt22"/>
    

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);
    }
}

你可能感兴趣的:(Android,android,移动开发,activity)