第一次開啟程式時姓名 , 電話與住址是空的 , 再輸入姓名電話及住址後離開 , 接下來進入程式時會顯示所輸入的值 , 程式碼如下。
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"; private EditText name; private EditText phone; private EditText address; private Button end; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (EditText) this.findViewById(R.id.name); phone = (EditText) this.findViewById(R.id.phone); address = (EditText) this.findViewById(R.id.address); end = (Button) this.findViewById(R.id.end); end.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); SharedPreferences sf = getSharedPreferences(PREF_NAME, 0); name.setText(sf.getString(key01, "")); phone.setText(sf.getString(key02, "")); address.setText(sf.getString(key03, "")); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); SharedPreferences sf = getSharedPreferences(PREF_NAME,0); SharedPreferences.Editor edit = sf.edit(); edit.putString(key01, name.getText().toString()); edit.putString(key02, phone.getText().toString()); edit.putString(key03, address.getText().toString()); edit.commit(); } }
Next, layout.xml
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....
沒有留言:
張貼留言