2.輸入程式
package com.example.sum;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SumActivity extends Activity {
/** Called when the activity is first created. */
private String PREF_NUM1="NUM1";
private String PREF_NUM2="NUM2";
private String PREF_SUM="SUM";
EditText et1, et2;
Button bt;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(R.id.EditText01);
et2 = (EditText) findViewById(R.id.EditText02);
tv = (TextView) findViewById(R.id.TextView01);
bt = (Button) findViewById(R.id.Button01);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
double d1,d2;
d1 = Double.parseDouble(et1.getText().toString());
d2 = Double.parseDouble(et2.getText().toString());
tv.setText(Double.toString(d1+d2));
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
SharedPreferences pref = getSharedPreferences ("pref_apn" , MODE_PRIVATE);
Editor ed = pref.edit();
ed.putString(PREF_NUM1, et1.getText().toString());
ed.putString(PREF_NUM2, et2.getText().toString());
ed.putString(PREF_SUM, tv.getText().toString());
ed.commit();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
SharedPreferences pref = getSharedPreferences("pref_apn" , MODE_PRIVATE);
et1.setText(pref.getString(PREF_NUM1, ""));
et2.setText(pref.getString(PREF_NUM2, ""));
tv.setText(pref.getString(PREF_SUM, ""));
super.onResume();
}
}
3. 在編輯框內輸入資料,然後離開程式,再執行程式,您會發現上次輸入狀態還在。
沒有留言:
張貼留言