package com.example.sharedpreferences;
import java.util.Map;
import com.eboy.service.PreferencesService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edtName;
private EditText edtAge;
private PreferencesService service = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = (EditText)findViewById(R.id.name);
edtAge = (EditText)findViewById(R.id.age);
service = new PreferencesService(getApplicationContext());
// 软件启动时加载保存的参数
Map<String, String> params = service.getPreferences();
edtName.setText(params.get("name"));
edtAge.setText(params.get("age"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* 保存参数,OnClick配置在xml文件中
* @param v
*/
public void save(View v){
String name = edtName.getText().toString();
String age = edtAge.getText().toString();
service.save(name, Integer.valueOf(age));
Toast.makeText(getApplicationContext(), "成功".toString(), Toast.LENGTH_SHORT).show();
}
}
import java.util.Map;
import com.eboy.service.PreferencesService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edtName;
private EditText edtAge;
private PreferencesService service = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = (EditText)findViewById(R.id.name);
edtAge = (EditText)findViewById(R.id.age);
service = new PreferencesService(getApplicationContext());
// 软件启动时加载保存的参数
Map<String, String> params = service.getPreferences();
edtName.setText(params.get("name"));
edtAge.setText(params.get("age"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* 保存参数,OnClick配置在xml文件中
* @param v
*/
public void save(View v){
String name = edtName.getText().toString();
String age = edtAge.getText().toString();
service.save(name, Integer.valueOf(age));
Toast.makeText(getApplicationContext(), "成功".toString(), Toast.LENGTH_SHORT).show();
}
}