Activity界面基本实验

Activity界面基本实验,第1张

Activity界面基本实验

【实验名称】实验二、Activity界面基本实验
【实验目的】
1、 掌握Activity的基本功能;
2、 掌握preference的基本功能;
3、 掌握断点的设置,调试程序;
【实验内容】
任务1:通过intent实现跳转,完成Activity之间的跳转;
任务2:intent数据的传递;
任务3:采用用preference实现随数据的存储;
任务4:掌握在虚拟机和真机环境下,对程序的调试;

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

这里我写了两种方法,一种是通过intent传送数据,一种是用preference传送数据,不过个人感觉,这两种方法都比较像,给我一种使用map的感觉,以后再看原理吧

MainActivity
package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
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 AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                EditText editText1 = findViewById(R.id.text_1);
                EditText editText2 = findViewById(R.id.text_2);
                EditText editText3 = findViewById(R.id.text_3);

//                String name = editText1.getText().toString();
//                intent.putExtra("name1", name);
//                name = editText2.getText().toString();
//                intent.putExtra("name2", name);
//                name = editText3.getText().toString();
//                intent.putExtra("name3", name);

                SharedPreferences sharedPreferences = getSharedPreferences("text", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("name1", editText1.getText().toString());
                editor.putString("name2", editText2.getText().toString());
                editor.putString("name3", editText3.getText().toString());
                editor.commit();
                startActivity(intent);
            }
        });
    }

}
SecondActivity
package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView textView1 = findViewById(R.id.number);
        TextView textView2 = findViewById(R.id.number2);
        TextView textView3 = findViewById(R.id.number3);
        
        SharedPreferences sharedPreferences = getSharedPreferences("text", Context.MODE_PRIVATE);
        String s1 = sharedPreferences.getString("name1", "暂无");
        String s2 = sharedPreferences.getString("name2", "暂无");
        String s3 = sharedPreferences.getString("name3", "暂无");

        textView1.setText(s1);
        textView2.setText(s2);
        textView3.setText(s3);

    }
}

activity_main




    

    

    

        

activity_second




    

    

    

        

ps:足足花了半天时间才把环境装好,针不戳,学这东西各种找资料,好麻烦啊

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4695056.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-07
下一篇2022-11-07

发表评论

登录后才能评论

评论列表(0条)

    保存