2012-05-22 21 views

回答

0

这样做的一种方法就是在您的活动中使用OnTouchListener。您可以准确检测屏幕上触摸的位置。

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class AndroidTestActivity extends Activity implements OnTouchListener { 
    LinearLayout main; 

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

    main = (LinearLayout) findViewById(R.id.main_layout); 
    main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area. 
    } 

    public boolean onTouch(View v, MotionEvent e) { 
    if(e.getX() <= main.getWidth()/5) { 
     Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show(); 
     return true;  
    } 

    return false; 
    } 
} 

您还需要标识主布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
+0

谢谢您的回答,但它似乎并没有为我工作。即使我没有设置任何条件,但我点击布局的任何区域动画没有开始 –

+0

20%区域我仍然有其他组件,但是我点击20%区域内的组件,它会做组件自己的功能,而不是开始动画 –

+0

如果您有任何其他布局,图像视图或任何其他地方,你需要添加侦听器与setOnTouchListener他们.. – Jan

相关问题