たぶんSharedPreferences
は、あなたが任意のイベントハンドラメソッドなどの内部でこのスニペットを入れ
探しているものですButton
さんOnClickListener
:
//creating an instance of a shared preference with code 'edit_text_code' and only reachable by this application
SharedPreferences mySharedPrefs=getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
//getting the mySharedPrefs's editor for further editing
SharedPreferences.Editor editor=settings.edit();
//putting the EditText content as a shared preference to be 'commited'
editor.putString("edit_text_code",myEditText.getText().toString());
editor.commit();
そして今、あなたは、この場合の 『共有好み』と内部の好みを、読んでください、あなたのEditText
内容:
例えば
public class MainActivity2 extends AppCompatActivity {
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
myTextView = (TextView) findViewById(R.id.my_text_view);
SharedPreferences mySharedPrefs = getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
String content = mySharedPrefs.getString("edit_text_code","Show this text in case that shared preference doesn't exist");
textView.setText("The EditText content was: "+ content);
}
他のトピックの詳細については、このブログをチェックアウト: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
もAndroidの開発については、このChalkSreetのアプリを試してみてください。 https://play.google.com/store/apps/details?id=com.chalkstreet.learnandroid&hl=en あり、あなたの質問


に非常によく似たものを含め、興味深い例を見つけることができます編集
ここで私が話していた例のソースがあり、それはうまくいくはずです。
public class SharedActivity extends AppCompatActivity {
EditText et;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_shared);
et = (EditText) findViewById(R.id.edit_text_shared);
SharedPreferences settings = getSharedPreferences("PREFS",MODE_PRIVATE);
et.setText(settings.getString("option",""));
}
//This does the trick
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences("PREFS",0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("option",et.getText().toString());
editor.commit();
}
}
OR あなたは私はあなたを助けることができるように、「変数」
はあなたのプログラムを添付してくださいすることができますことを保存するために、書き込み用のJava
OutputStream
を使用してファイルを作成し、InputStream
クラスができ –使用して環境設定を共有したりSQLiteデータベース。バックエンドの使用を除外している場合に、アプリケーションが閉じられた後に保存されたデータを復元するオプションです。 – portfoliobuilder