如何在类和onCreate()之间传输数据?

时间:2014-03-21 00:39:32

标签: android android-activity

我在onCreate()方法中有一个int,我想把int转移到一个类中。我该怎么做?

protected void onCreate(Bundle savedInstanceState) {
int testInt = R.drawable.test;

}

public class myapplicationtest{
//I would like the int to be here
}

请注意,它们都处于相同的活动中。

2 个答案:

答案 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;
        }
    }
}