2013-10-23 74 views
0

你好我試圖使用OnTouchListener。onTouchListener根本不工作

我的問題是,它似乎工作。

我dotn得到任何日誌調用。

這是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.check); 

     comenntsBigLayout = (RelativeLayout) findViewById(R.id.comenntsBigLayout); 
     sendComment = (Button) findViewById(R.id.sendComment); 
     commentsLayout = (LinearLayout) findViewById(R.id.commentsLayout); 
     enterComment = (EditText) findViewById(R.id.enterComment); 
     imagesGrid = (GridView) findViewById(R.id.imagesGrid); 
     mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); 

     mainLayout.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       Log.i("test", "test"); 
       if(event.getAction() == MotionEvent.ACTION_UP) 
       { 
        Log.i("test", "test1"); 
        comenntsBigLayout.setVisibility(View.VISIBLE); 
        imagesGrid.setVisibility(View.GONE); 
       } 
       else if(event.getAction() == MotionEvent.ACTION_DOWN) 
       { 
        Log.i("test", "test2"); 
        comenntsBigLayout.setVisibility(View.GONE); 
        imagesGrid.setVisibility(View.VISIBLE); 
       } 

       return true; 
      } 
     }); 
} 

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <GridView 
     android:id="@+id/imagesGrid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:numColumns="3" > 
    </GridView> 

    <RelativeLayout 
     android:id="@+id/comenntsBigLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/sendComment" 
      android:layout_marginBottom="30sp" 
      android:layout_marginLeft="15sp" 
      android:layout_marginRight="15sp" 
      android:layout_marginTop="15sp" 
      android:background="@color/Beige" > 

      <LinearLayout 
       android:id="@+id/commentsLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@+id/sendComment" 
       android:background="@color/Beige" 
       android:orientation="vertical" > 
      </LinearLayout> 
     </ScrollView> 

     <EditText 
      android:id="@+id/enterComment" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="10sp" 
      android:layout_marginLeft="5sp" 
      android:layout_toLeftOf="@id/sendComment" 
      android:hint="Write something..." 
      android:textColorHint="@color/black" /> 

     <Button 
      android:id="@+id/sendComment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:text="Send" 
      android:textColor="@color/black" /> 
    </RelativeLayout> 

</RelativeLayout> 
+0

會發生什麼呢? – Rajj

+0

當您單擊相對佈局時,其他任何窗口小部件都會關注它 – Raghunandan

回答

1

你的其他UI元素都在你試圖使用與觸摸相對佈局的頂部,將OnTouchListeneter到其他視圖。

0

試試這個,

mainLayout.setOnTouchListener(new View.OnTouchListener() 
{ 
    // Implementation; 
});