我正在绝望地尝试从扩展ImageView的Activity类传递一个int(我正在修改标准ImageView的类)。这似乎很难。到目前为止我试图:
我想知道是否有任何可能的方法将一个int从一个Activity类传递给一个扩展ImageView的类。我没有上面列出的解决方案的代码,但我已经尝试了从Activity到Activity。有没有办法传递这个int?
答案 0 :(得分:2)
我想你在代码中的某处初始化了'extends ImageView'类,用它来扩展布局?即:
CustomImageView myImageView = new CustomImageView();
[...]
yourRootView.addView(myImageView);
如果是这样,你可以在初始化时传递int:
CustomImageView myImageView = new CustomImageView(yourInt);
使用CustomImageView.class在cunstructor中捕获它:
public CustomImageView(int yourInt) {
Log.i("Hello", "This is the int your were looking for" + yourInt);
另一种方法是在CustomImageView.class中设置setter / getter
private yourInt;
public void setInt(int yourInt) {
this.yourInt = yourInt;
}
然后,您可以根据自己的活动执行以下操作:
CustomImageView myImageView = new CustomImageView();
myImageView.setInt(yourInt);
很抱歉,如果这不能回答你的问题,你提供的信息很少,所以我不得不猜(*编辑,我找不到'评论'按钮只是...评论)
修改
您的活动课程:
class MyActivtiy extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_someactivity);
MyImageView myImageView = new MyImageView(2); // 2 is our Example Integer
//**Alternative** - Needs setInger(int someInt) in ImageView class (see below)
myImageView.setInteger(4);
}
}
您的ImageView类
class MyImageView extends ImageView {
Int anInteger;
public MyImageView(int anInteger) {
Log.i("Hello", "The integer is: " + anInteger);
// Above line will show in Logcat as "The integer is: 2
this.anInteger = anInteger;
// Above line will set anInteger so you can use it in other methods
}
public void printIntToLogCat() {
Log.i("Hello", "You can use the integer here, it is: " + anInteger);
// Above line in logcat: "You can [...], it is: 2"
}
//**Alternative**
public void setInteger(int someInt) {
this.anInteger = someInt; // With above example this will set anInteger to 4
printIntToLogCat();
// Above Line will print: "You can[...], it is: 4"
}
}