[quote=引用 3 楼 u010200419 的回复:] [quote=引用 1 楼 u010200419 的回复:] 用sharepreference保存
用sharepreference保存
1为开2为关 不就行了 退出的时候或者改变开关状态的时候 就存进去 在onCreate 的时候先去读状态 如果是2 或者你第一次开程序 还没写过 那就读不到 会去一个默认值值 你也设成2 这开关就设置成关 1就设置成开 还不会怎么代码改变开关状态 你百度android 改变开关状态
[quote=引用 1 楼 u010200419 的回复:] 用sharepreference保存
public class SpActivity extends Activity { private EditText nameText; private EditText ageText; private TextView resultText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)this.findViewById(R.id.name); ageText = (EditText)this.findViewById(R.id.age); resultText = (TextView)this.findViewById(R.id.showText); Button button = (Button)this.findViewById(R.id.button); Button showButton = (Button)this.findViewById(R.id.showButton); button.setOnClickListener(listener); showButton.setOnClickListener(listener); // 回显 SharedPreferences sharedPreferences=getSharedPreferences("ljq123", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); String nameValue = sharedPreferences.getString("name", ""); int ageValue = sharedPreferences.getInt("age", 1); nameText.setText(nameValue); ageText.setText(String.valueOf(ageValue)); } private View.OnClickListener listener = new View.OnClickListener(){ public void onClick(View v) { Button button = (Button)v; //ljq123文件存放在/data/data/<package name>/shared_prefs目录下 SharedPreferences sharedPreferences=getSharedPreferences("ljq123", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); switch (button.getId()) { case R.id.button: String name = nameText.getText().toString(); int age = Integer.parseInt(ageText.getText().toString()); Editor editor = sharedPreferences.edit(); //获取编辑器 editor.putString("name", name); editor.putInt("age", age); editor.commit();//提交修改 Toast.makeText(SpActivity.this, "保存成功", Toast.LENGTH_LONG).show(); break; case R.id.showButton: String nameValue = sharedPreferences.getString("name", ""); int ageValue = sharedPreferences.getInt("age", 1); resultText.setText("姓名:" + nameValue + ",年龄:" + ageValue); break; } } }; }
百度android sp存储 1.写内容 sp = getSharedPreferences("UserInfo", 0); sp.edit().putBoolean("cbal", isChecked).commit(); UserInfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“isChecked”的值,首先通过SharedPreferences的edit()方法创建editor,然后调用commit()方法提修改 2.读内容 sp = getSharedPreferences("UserInfo", 0); etAccount.setText(sp.getString("account", null)); etPW.setText(sp.getString("password", null)); cbal.setChecked(sp.getBoolean("cbal", false)); cbrp.setChecked(sp.getBoolean("cbrp", false));
80,490
社区成员
91,375
社区内容
加载中
试试用AI创作助手写篇文章吧