2016-12-25 2 views
0

변수가있어서 처음으로 앱을 시작하는 경우에만 변수를 변경하고 싶습니다. 예 :Android : 변수를 저장하는 방법은 무엇입니까?

  1. 변수를 초기화하지 않았습니다. 4.
  2. 에 변수 변화는 내가 응용 프로그램 변수가 너무 4.

그리고 열 수 있습니다 응용 프로그램을

  • 을 닫습니다.

    어떻게하면됩니까?

  • 답변

    1

    여러분은 Android를 처음 사용하신 것 같습니다. "SharedPreferences"를 사용하여 값을 저장할 수 있습니다.

    확인이 링크 : https://developer.android.com/training/basics/data-storage/shared-preferences.html

    는 또한이 예제를 체크 아웃 :

    https://www.tutorialspoint.com/android/android_shared_preferences.htm

    당신이 코드를 사용할 수 있습니다 여기에

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);  
    Editor editor = sharedpreferences.edit(); 
    editor.putString("key", "value"); 
    editor.commit(); 
    

    는 MyPREFERENCES은 파일 이름입니다. "value"를 변수 이름으로 대체하십시오. 당신이 값을 검색하려면 는 다시 다음 사용

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    prefs.getBoolean("key", true); 
    //There are other methods like getInt(),getDouoble().getString() depending on the type of value 
    
    +0

    나는 링크를 수정했다. 지금 확인해보세요! 샘플 코드도 추가했습니다. – Raman

    +0

    MyPreferences가 file.txt입니까? – Curio

    +0

    예, 키와 값이 저장되는 곳입니다. 어떤 이름이라도 제공 할 수 있습니다. – Raman

    0

    당신은 또한 로컬 파일에 기록하고 큰 개체의 데이터를 유지하려는 경우 그 목적을 위해 SQLite는 사용할 수 있습니다.

    관련 문제