触摸功能屏幕与否

时间:2013-11-27 09:11:01

标签: java android eclipse screen

我找不到解决方案...... 如果没有触摸屏幕,我想运行firstfunc() 我有firstfunc()我不想在触摸屏幕时运行它

对于它我需要布尔函数返回true或false以确定是否触摸屏幕。 我尝试了myView.setOnTouchListener(

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

但这不是我想要的......

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

  1. 在设置标志的OnTouchListener上实施。
  2. 将#1的事件设置为应该感应触摸的视图。
  3. 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; 
          }
     }