package COM.TQC.GDD02;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class GDD02 extends Activity {
public static final String PREF_NAME = "GDD02_PREF";
public static final String key01 = "key01";
public static final String key02 = "key02";
public static final String key03 = "key03";
// 加入以下的 constant
public static final int MODE_WORLD_WRITTABLE = 0;
private EditText EditText01;
private EditText EditText02;
private EditText EditText03;
// 多加入一個 button 宣告
private Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取出所有元件
EditText01 = (EditText) findViewById(R.id.editText1);
EditText02 = (EditText) findViewById(R.id.editText2);
EditText03 = (EditText) findViewById(R.id.editText3);
bt = (Button) findViewById(R.id.button1);
// 由 SharedPreferencs 取出內容,
SharedPreferences st = getSharedPreferences(PREF_NAME,0);
String value01 = st.getString(key01, "");
String value02 = st.getString(key02, "");
String value03 = st.getString(key03, "");
// Assigne 至 EditText 內
EditText01.setText(value01);
EditText02.setText(value02);
EditText03.setText(value03);
// button 指定 OnClickListener
// 背下這個語法
bt.setOnClickListener(new Button.OnClickListener()
{
// 沒有 @Override
// 記得用 public void
public void onClick(View v)
{
finish();
}
}
);
}
// 記得用 @Override
// 得要用 protected void
// 同時要作 super.onStop();
@Override
protected void onStop() {
super.onStop();
SharedPreferences st = getSharedPreferences(PREF_NAME,MODE_WORLD_WRITTABLE);
SharedPreferences.Editor editor = st.edit();
editor.putString(key01, "" + EditText01.getText());
editor.putString(key02, "" + EditText02.getText());
editor.putString(key03, "" + EditText03.getText());
editor.commit();
}
}
沒有留言:
張貼留言