我在onCreate()方法中有一个int,我想把int转移到一个类中。我该怎么做?
protected void onCreate(Bundle savedInstanceState) {
int testInt = R.drawable.test;
}
public class myapplicationtest{
//I would like the int to be here
}
请注意,它们都处于相同的活动中。
答案 0 :(得分:1)
试试这个..
public static int testInt;
protected void onCreate(Bundle savedInstanceState) {
testInt = R.drawable.test;
}
和
public class myapplicationtest{
//I would like the int to be here
int value = OnCreateClassName.testInt;
}
其中OnCreateClassName
是 testInt
答案 1 :(得分:1)
在您的活动中,创建包含testInt的类的实例,并通过setter设置其值。例如,
public class MyActivity extends Activity {
private myapplicationtest mytestInst = new myapplicationtest();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mytestInst.setInt(R.drawable.test);
}
public class myapplicationtest{
private int testInt = 0;
public void setInt(value) {
testInt = value;
}
public int getInt() {
return testInt;
}
}
}