activity2编辑内容存到本地,返回activity1传送内容。SharedPreferences.

package com.example.data;

import android.content.Context;
import android.content.SharedPreferences;

public class dataStorage {
    public static SharedPreferences share(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("date", Context.MODE_PRIVATE);
        return sharedPreferences;
    }
    public static Object getContent(Context context){
        return share(context).getString("name",null);
    }
    public static void setContent(String name, Context context){
        SharedPreferences.Editor e = share(context).edit();
        e.putString("name",name);
        e.apply();
    }
}

---------------------工具类----------------------

package com.example.data;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    dataStorage datastorage;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = findViewById(R.id.tv1);
        datastorage=new dataStorage();
        startActivity(new Intent(MainActivity.this,second.class));

    }
protected void onRestart(){
        super.onRestart();
    if(datastorage.getContent(this)!=null){
        tv1.setText(datastorage.getContent(this).toString());
    }
}
}

----------------activity1--------------------

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100GB!"
        android:textColor="@color/black"/>

</LinearLayout>

------------activity1.xml----------------------

package com.example.data;

import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class second extends AppCompatActivity {
    EditText et1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        dataStorage datastorage;
        datastorage = new dataStorage();
        et1= findViewById(R.id.et1);
        if(datastorage.getContent(this)!=null){
            et1.setText(datastorage.getContent(this).toString());
        }

    }
    protected void onPause(){
        super.onPause();
        dataStorage.setContent(et1.getText().toString(),this);
    }
}

----------------------activity2-----------------------

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

    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0GB!"
        android:textColor="@color/black"/>

</LinearLayout>

------------------activity2.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值