2013-11-27 222 views
0

我不能找到解决办法...... 我想运行firstfunc()如果屏幕不触及
我有firstfunc()我不想跑,如果屏幕被触摸功能屏幕被触摸

它需要布尔函数返回true或false来确定屏幕是否被触摸。 我试图和myView.setOnTouchListener(

public boolean onTouchEvent(MotionEvent event) { 
      // TODO Auto-generated method stub 
      return super.onTouchEvent(event); 
      } 

但它不是要我要...

+0

你想要什么到底是什么?只有在触摸屏幕时才会调用“onTouchEvent”,否则屏幕不会被触摸! – user2652394

+0

我想先运行func()如果屏幕没有被触及 – NickUnuchek

+1

基本上你不能,你怎么知道什么时候没有触摸屏幕,屏幕只是一个工具来接收来自用户的信号TOUCH,如果用户不触碰它根本没有信号,所以屏幕基本上一直没有被触摸。我很抱歉,但你不能那样做。干杯! – user2652394

回答

0

试试这个:

public class MyActivity extends Activity implements OnTouchListener 
{ 

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

    //the whole screen becomes sensitive to touch 
    mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main); 
    mLinearLayoutMain.setOnTouchListener(this); 
} 

public boolean onTouch(View v, MotionEvent event) 
{ 
    // put your code in here 

    return false;//false indicates the event is not handled 
} 

}

在layout.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout_main"> 
    <!-- your other views go here--> 

</LinearLayout> 
+0

我有 FUNC(){ } 我不想跑,如果屏幕被触摸 – NickUnuchek

0
  1. 执行OnTouchListener其中设置了一个标志。
  2. 将#1的事件设置为应检测触摸的视图。

class MyActivity extends Activity implements OnTouchListener

View senceTouch; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     senceTouch = findViewById(R.id.view_id); 
     senceTouch.setOnTouchListener(this) 

    } 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
     // set flag here 
     return true; 
     } 
} 
+0

如果我触摸屏标志=真; 如果我不触屏如何设置flag = false? – NickUnuchek

+0

在类级别声明布尔型标志,默认情况下其值为false。标志的错误值将指示屏幕未被触摸。 – MAkS