저는 Android 앱을 작성 중이므로 사용자가 환경 설정에서 버튼을 클릭 한 다음 다른 클래스에서 해당 환경 설정을 검색하는 시간을 저장하고 싶습니다. 나는이 시점에서 완전한 초보자이며 어떤 도움을 주시면 감사하겠습니다.기본 설정으로 버튼 클릭 수를 저장합니다.
0
A
답변
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
// 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);
관련 문제
- 1. 버튼 클릭 수를 페이지 뷰로
- 2. SiteCatalyst에서 버튼 클릭 수를 계산합니다.
- 3. 기본 설정으로
- 4. 자바 스크립트에서 localStorage의 버튼 클릭 횟수를 저장합니다.
- 5. Oracle에 utf-8 문자를 기본 NLS_CHARACTERSET 설정으로 저장합니다.
- 6. 버튼 클릭 수를 계산하는 PHP 스크립트
- 7. 클릭 이벤트를 사용자 정의 기본 설정으로 등록 할 수 없습니다.
- 8. 비활성화 기본 버튼 클릭 효과
- 9. tkinter는 최대 버튼 클릭 수를 설정합니다
- 10. ajax가 포함 된 버튼 클릭 수를 계산합니다.
- 11. 자바의 버튼 클릭 수를 계산하는 방법
- 12. googleplusone 버튼 클릭 수를 추적하는 방법은 무엇입니까?
- 13. 버튼 클릭 수를 센다 데이터베이스에 삽입하십시오 PHP
- 14. Windsor DoNotWire 기본 설정으로
- 15. 자바의 기본 설정으로 대체
- 16. 기본 설정으로 라디오 버튼을 true로 설정하는 방법
- 17. 시뮬레이터가 이전 기본 설정으로 유지됩니다. 기본 폼
- 18. IntelliJ 안전 삭제 기본 설정으로
- 19. 기본 설정으로 텍스트를 설정하는 방법
- 20. drupal 기본 설정으로 변경하는 방법
- 21. 설정으로 기본 메인 프로그램을 변경하십시오.
- 22. 기본 버튼 클릭 금지 JQuery Mobile
- 23. 기본 클래스에 참조를 저장합니다.
- 24. pdf.js 핸드 툴을 기본 설정으로 사용하려면 어떻게합니까?
- 25. 버튼 클릭 유지 클릭
- 26. 버튼 배열의 버튼 클릭
- 27. JQuery를 사용하여 각 변수의 수를 저장합니다.
- 28. javascript 버튼 클릭 및 asp.net 버튼 클릭
- 29. 클릭 버튼 클릭 변경 버튼 값 텍스트
- 30. 버튼을 클릭 할 때 전화 설정으로 이동
이봐,이 코드를 시도하자 나도 알아, 아직도 그것에 문제가 있다면 .. –