2013-10-16 4 views
0

저는 Android 앱을 작성 중이므로 사용자가 환경 설정에서 버튼을 클릭 한 다음 다른 클래스에서 해당 환경 설정을 검색하는 시간을 저장하고 싶습니다. 나는이 시점에서 완전한 초보자이며 어떤 도움을 주시면 감사하겠습니다.기본 설정으로 버튼 클릭 수를 저장합니다.

답변

1

이 방법을 수행 ..

**Activity1.java** 
------------------ 

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
int myIntValue = sp.getInt("your_int_key",0); 


     yourbutton.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 

        editor.putInt("your_int_key",++myIntValue); 
           editor.commit(); 
        } 
       }); 

**Activity2.java** 
----------------- 

    SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); 
int myIntValue = sp.getInt("your_int_key", 0); 
+0

이봐,이 코드를 시도하자 나도 알아, 아직도 그것에 문제가 있다면 .. –

0
// declare thsi class variable in class from where u will put the string u wanna store in shared pref 

//class variables 

    SharedPreferences pref; 
    SharedPreferences.Editor editor; 
------------------- 

//in oncrete method 
// declare this in oncreate method 

     pref = getSharedPreferences("testapp", MODE_PRIVATE); 
     editor = pref.edit(); 

// the varibale u wanna put use the below statements 
// for string use putString 
// for boolean as u need use putBoolean 
// have a look at the various option it offers.. 


editor.putString("selected", "nil"); 
editor.commit(); 


// here is the statement use this statement in class where u wanna retireve ur strings 
// use getBoolean for Boolean variables 

pref.getString("selected", "nil") 

// here in sceond parameter in above statement is : if the value u r requesting for that is specified in first parameter is not present then it will return the //value which is your second parameter.. 
0

이전 버튼의 상태를 저장하는 한 가지 방법은 Android에서 공유 환경 설정을 사용하는 것입니다. 공유 환경 설정을 사용하면 나중에 쉽게 검색 할 수있는 키 값 쌍의 데이터를 저장할 수 있습니다. Android에는 데이터 액세스 메커니즘 중 하나가 있습니다. 기타 SqlLite 데이터베이스 & 파일. 이제 다시 문제에오고 다시 공유 환경

공유 선호

비디오에

안드로이드 문서. 한 번 체크 버튼의 상태를 저장해야했습니다. 그런 다음 나중에 다시 액세스하십시오 (귀하가 원하는 것과 비슷한 것으로 보입니다)

Part 1 Accessing Share preference Values : 

     public static final String PREFS_FILE = "MyPreferences"; 
     public static final String PREFS_NAME = "USER_NAME"; 
     public static final String PREFS_CHOICE = "USER_CHOICE"; 

     SharedPreferences sp; 

     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      chkChoice = (CheckBox)findViewById(R.id.chkChoice); 
      btnMain = (Button)findViewById(R.id.btnMain); 
      btnMain.setOnClickListener(this); 

      // Here i access the shared preference file . 
      sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE); 
      // If i have a preference for my checkbox saved then load it else FALSE(unchecked) 
      chkChoice.setChecked(sp.getBoolean(PREFS_CHOICE, false)); 
     } 


    Part 2 Setting Share preference from your activity : 

    sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 

    editor.putString(PREFS_NAME, txtName.getText().toString()); 
    editor.putBoolean(PREFS_CHOICE, chkChoice.isChecked()); 

    editor.commit(); 

    // Close the activity to show that the data is still saved 
    finish(); 

위의 내용은 확인란입니다. 저장하려는 종류의 버튼 정보에 맞게 조정해야합니다. 희망이 당신을 시작 가져옵니다. 다음과 같은

1

시도 뭔가 :

Activity1.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Activity1.this); 
SharedPreferences.Editor editor = app_preferences.edit(); 
int i=0; 

    yourbutton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
            i++; 
        editor.putInt("counter", i); 
        editor.commit(); 
       } 
      }); 

Activity2.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this); 
String counter = app_preferences.getInt("counter", 0); 
관련 문제