gpt4 book ai didi

java - 在 OnTouch 中更改页面

转载 作者:行者123 更新时间:2023-12-01 15:22:51 24 4
gpt4 key购买 nike

我知道如何制作它,这样您就可以在 onClick 上打开一个新页面,但是当我将相同的代码放入 onTouch 中时,它不起作用?这是我的代码...如果我单击错误,我会收到一条消息,提示“构造函数 Intent(Tab3, Class) 未定义”,它希望我将其设置为“= new Intent”,但对我有任何帮助吗?这对我来说没有意义。

public boolean onTouch(View v, MotionEvent event) 
{
if(rect2.contains((int) event.getX(),(int) event.getY()))
{
Intent SectionManager = new Intent (this, SectionManager.class);
startActivity (SectionManager);
}
return true;
}

编辑有关我的代码的更多信息!

public class Tab3 extends View implements OnTouchListener
{
LinearLayout myLayout;
int x1, x2, y1, y2, x, y;
// x, y;
Rect rect2 = new Rect();

public Tab3(Context context, AttributeSet attrs)
{
super (context, attrs);

myLayout = (LinearLayout) findViewById(R.id.myLayout);

x1 = 0;
x2 = 300; //1280 by 748
y1 = 0;
y2 = 300;




setFocusable(true);
setFocusableInTouchMode(true);
setOnTouchListener(this);
requestFocus();
}

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

Paint blue = new Paint();
blue.setColor(Color.BLUE);
canvas.drawRect(rect2, blue);
rect2.set(x1, y1, x2, y2);

invalidate();
}

@Override
public boolean onTouch(View v, MotionEvent event)
{
if(rect2.contains((int) event.getX(),(int) event.getY()))
{
Intent SectionManager = new Intent (Tab3.this, SectionManager.class);
startActivity (SectionManager);
}
return true;
}
}

最佳答案

您的上下文是错误的,您需要在其中使用YourClassName.this,而不仅仅是this。目前 this 表示 View 又名 Tab3,它不是上下文

public class MyActivity extends Activity{
.
all your activity stuff here
.
.
public class Tab3 extends View implements OnTouchListener{

LinearLayout myLayout;
int x1, x2, y1, y2, x, y;
// x, y;
Rect rect2 = new Rect();

public Tab3(Context context, AttributeSet attrs)
{
super (context, attrs);

myLayout = (LinearLayout) findViewById(R.id.myLayout);

x1 = 0;
x2 = 300; //1280 by 748
y1 = 0;
y2 = 300;




setFocusable(true);
setFocusableInTouchMode(true);
setOnTouchListener(this);
requestFocus();
}

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

Paint blue = new Paint();
blue.setColor(Color.BLUE);
canvas.drawRect(rect2, blue);
rect2.set(x1, y1, x2, y2);

invalidate();
}

@Override
public boolean onTouch(View v, MotionEvent event)
{
if(rect2.contains((int) event.getX(),(int) event.getY()))
{
Intent SectionManager = new Intent (MyActivity.this, SectionManager.class);
startActivity (SectionManager);
}
return true;
}
} //end Tab3 Class
} //end MyActivity

Tab3 现在是 MyActivity 的子类,您应该能够执行 MyActivity.this

关于java - 在 OnTouch 中更改页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626117/

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