温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

abdroid的activity保存状态

发布时间:2020-07-18 03:12:18 来源:网络 阅读:283 作者:matengbing 栏目:移动开发
package com.example.android.active;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity6 extends Activity{
	private EditText editText;
	private Button button;
	private SharedPreferences sharedPreferences;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main6);
		button=(Button) findViewById(R.id.test);
		editText=(EditText) findViewById(R.id.EditText_main6_1);
		//获取共享属性操作的工具(文件名,操作模式)
		//MODE_PRIVATE/0,私有模式,只能本应用访问
		//MODE_WORLD_READABLE(读)和MODE_WORLD_WRITEABLE(写)不建议使用(其他因公可以直接读取)
		sharedPreferences=this.getSharedPreferences("data", 0);
		
	}
	//在onPause()方法中编写保存数据的代码
	//还原在onResume()方法中
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//读取信息数据(从xml文件中),写入editor
		String msg=editText.getText().toString();
		Editor editor=sharedPreferences.edit();
		editor.putString("msg", msg);
		editor.commit();
	}
	//在onResume()方法中编写还原代码
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		editText.setText(sharedPreferences.getString("msg", ""));
		/*
		 * 删除信息数据(文件中的信息)
		Editor editor=sharedPreferences.edit();
		editor.clear();
		editor.commit();
		*/
	}
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI