gpt4 book ai didi

Android ontouchlistener帮助

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

我想知道,如果我在程序中有两个 Activity ,那么如何使用 ontouchlistener 在两个 Activity 之间切换,只需触摸屏幕上的任意位置?

public class V19 extends Activity implements OnTouchListener{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay19);
}

@Override
public boolean onTouch(View to_main, MotionEvent event) {
return false;
}
}

最佳答案

您可以使用布局充气器和方法 setContentView(View v)

public class V19 extends Activity implements OnTouchListener{@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    View view = getLayoutInflater().inflate(R.layout.lay19, null);    setContentView(view);    view.setOnTouchListener(this);}@Overridepublic boolean onTouch(View to_main, MotionEvent event) {    Intent i = new Intent(this, Activity2.class);    startActivity(i);    return false;}}    

而且你可以捕获所有触摸事件。

另一种方法是覆盖 public boolean dispatchTouchEvent (MotionEvent ev) 方法。引用说:

public boolean dispatchTouchEvent (MotionEvent ev)

Since: API Level 1 Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally. Parameters

ev The touch screen event.

Return true if this event was consumed.

关于Android ontouchlistener帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6597358/

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