gpt4 book ai didi

android - 将 Activity 的 Intent 传递给扩展表面 View 的类?

转载 作者:行者123 更新时间:2023-11-30 04:21:36 25 4
gpt4 key购买 nike

如何将 intent 从 Activity 传递到扩展 SurfaceView 的类?

这是发送 Intent 的代码:

btnCutOutter.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Send intent to cut image from user selection path
//See designCanvas.java for details

String optionSelected = "Yes";
Intent myIntent = new Intent(view.getContext(), designCanvas.class);
myIntent.putExtra("id",optionSelected );

}
});

当我尝试检索 Intent 数据时:

public class designCanvas extends SurfaceView implements SurfaceHolder.Callback {
//Create shape using Path
Path mPath = new Path();
private drawThread _thread;
private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();

public designCanvas(Context context, AttributeSet attributes) {
super(context, attributes);
getHolder().addCallback(this);
setFocusable(true);
_thread = new drawThread(getHolder(), this);
String optionSelected = getIntent().getStringExtra("id");

. . .

我被 eclipse 告知:

The method getIntent() is undefined 

如果有人能给我这样的指导,我将不胜感激:)

非常感谢。

最佳答案

android 中的任何 View 都包含在 Activity 中,因此 Activity 具有对 View (在本例中为 SurfaceView)的引用。因此,只需在 Activity 中获取 Intent 数据,然后对要传递给 SurfaceView 的数据使用 setter

因此,在包含 SurfaceView 的 Activity 中调用 getIntent 就像您在问题中遇到的那样,然后在 SurfaceView 中调用 setter,例如。 mySurfaceView.setSomeValue(someValue)

编辑

您正在 designCanvas 类中调用 getIntent(),它需要在包含 SurfaceView 的 Activity 中调用

public SomeActivity extends Activity{

public void onCreate(Bundle b){
super.onCreate(b);

//give that the constructor for your surface view has an attributeset i assume
//you are creating it in xml
designCanvas dc = findViewById(someid);

String s = getIntent(.getStringExtra("id");
dc.setId(s);

}

关于android - 将 Activity 的 Intent 传递给扩展表面 View 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9178568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com