Java 在OnTouch中更改页面
我知道如何使它,所以你会打开一个新的页面点击,但当我把相同的代码到onTouch它不工作?这是我的密码。。。如果我点击错误,我会得到一条消息说“构造函数意图(Tab3,类)未定义”,它希望我将其设置为“=newintent”,但这对我来说没有意义Java 在OnTouch中更改页面,java,android,android-intent,touch-event,Java,Android,Android Intent,Touch Event,我知道如何使它,所以你会打开一个新的页面点击,但当我把相同的代码到onTouch它不工作?这是我的密码。。。如果我点击错误,我会得到一条消息说“构造函数意图(Tab3,类)未定义”,它希望我将其设置为“=newintent”,但这对我来说没有意义 public boolean onTouch(View v, MotionEvent event) { if(rect2.contains((int) event.getX(),(int) event.getY()))
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;
}
}
你的上下文是错误的,你需要使用
你的类名。这个里面不仅仅是这个。当前此
表示视图akaTab3
,它不是上下文
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
us需要在onTouch中,所以我不确定,如果我删除需要在那里添加null
,那么我在“startActivity”上仍然存在错误说“类型Tab3的方法startActivity(Intent)未定义”阅读我的答案,了解你为什么说我应该有Intent SectionManager=new Intent(SectionManager.this,SectionManager.class)代码>?因为这只是给了我一个不同的错误。。。说“SectionManager类型的封闭实例在作用域中不可访问”不(您当前所在的类名为.this,SectionManager.class)
就是您想要的,您甚至可以在其中使用getApplicationContext()
不确定仅仅取决于您的代码我知道有Intent SectionManager=new Intent(表3.this,SectionManager.class);startActivity(SectionManager)
我仍然有两个错误和两个相同的错误…如果这是浪费您的时间,我很抱歉,但我真的需要解决这个问题。好的,这就是为什么它是一个视图,只需创建一个共享首选项变量或持久性的东西,并在应用程序启动时将其设置为上下文。或者,您可以将该视图类移动到活动类a中It’就像一个子类,那么你就可以通过这种方式获得上下文。你能给我举个例子吗?如果可能的话,我会非常感激的