나는 안드로이드에 대해 다음과 같이 도와주세요.android의 모든 클래스에서 액세스 할 수있는 변수를 선언하는 방법은 무엇입니까?
내가 선택한 라디오 버튼의 ID를 저장하는 정수 값을 가졌습니다. 유효성 검사를 위해 내 앱의 다양한 클래스에서이 값에 액세스해야합니다.
모든 클래스에서이 변수를 선언하고 액세스하는 방법을 알려주십시오.
감사합니다.
나는 안드로이드에 대해 다음과 같이 도와주세요.android의 모든 클래스에서 액세스 할 수있는 변수를 선언하는 방법은 무엇입니까?
내가 선택한 라디오 버튼의 ID를 저장하는 정수 값을 가졌습니다. 유효성 검사를 위해 내 앱의 다양한 클래스에서이 값에 액세스해야합니다.
모든 클래스에서이 변수를 선언하고 액세스하는 방법을 알려주십시오.
감사합니다.
U는 사용할 수 있습니다
MainActivity.class
Public static int myId;
을 다른 활동에서.
int otherId=MainActivity.myId;
geet 감사합니다,이 작품은 –
는이
class A{
static int a;
}
당신은이 같은 다른 클래스에서 액세스 할 수있는 모든 class.Like에 static
액세스로 정수 변수를 선언 할 수 있습니다. 싱글 톤 패턴 다음
class B{
int b = A.a;
}
u는 당신이해야 object.what는 새로운을 만들 클래스마다 인스턴스 (instance)를 만들 경우 자바/안드로이드 이러지 할 수있는 유일한 방법입니다
1.create a model class and make its as singleton
2.try to access the modelclass from every class
public class CommonModelClass
{
private static CommonModelClass singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private CommonModelClass()
{
// Optional Code
}
public static synchronized CommonModelClass getSingletonObject()
{
if (singletonObject == null)
{
singletonObject = new CommonModelClass();
}
return singletonObject;
}
/**
* used to clear CommonModelClass(SingletonClass) Memory
*/
public void clear()
{
singletonObject = null;
}
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
//getters and setters starts from here.it is used to set and get a value
public String getcheckBox()
{
return checkBox;
}
public void setcheckBox(String checkBox)
{
this.checkBox = checkBox;
}
}
다른 클래스의 모델 클래스 값에 액세스하기 commonModelClass = CommonModelClass.getSingletonObject();
commonModelClass.getcheckBox(); http://javapapers.com/design-patterns/singleton-pattern/
확인란 checkBox = (CheckBox) findViewById (R.id.checkbox1); 이 체크 박스 인스턴스를 sigleton에 저장하고 앱을 통해 액세스 할 수 있습니다. – Ruban
Ruban에 감사드립니다. public static int로 정수를 선언하여 액세스하려고했습니다. 하지만 당신이 지정한 기법을 모르겠습니다. 그러므로 역시 그렇게 해보 죠, 고마워요. –
정적을 사용하는 것도 좋은 일입니다.하지만 싱글 톤 패턴을 구현하는 것은 처음에는 U 비트를 구현하기 어렵게 만듭니다.하지만 한 번 사용하면 U를 사용하면 더 많은 이점을 찾을 수 있습니다 – Ruban
http://developer.android.com/reference/android/app/Application.html – Raghunandan
우선 체크 박스의 ID는 R.java 파일에 storen이므로 어디서나 액세스 할 수 있습니다. (저장할 필요가 없음). checkBox.isChecked()'checkBox.isChecked()'가 체크되어 있는지 확인하고, 아무 것도 저장할 필요가 없다. checkBox와 같이 checkBox를 선언하면된다. checkBox = (CheckBox) findViewById (R.id. checkbox1);를 호출하고 위의 함수를 사용하십시오. – g00dy