如何在android中的同一个Button上处理onClick和onTouch事件

时间:2014-05-20 09:20:56

标签: android

这是我的所有xml文件..........

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageButton 
       android:id="@+id/imageButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/ic_launcher"
       android:layout_centerInParent="true"/>

</RelativeLayout>

imagenext.xml

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


    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Image Captured"
        android:textSize="40sp"
        android:textStyle="bold|italic"    
        android:layout_centerInParent="true"/>

</RelativeLayout>

next.xml

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


    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:textSize="40sp"
        android:textStyle="bold|italic"    
        android:layout_centerInParent="true"/>

</RelativeLayout>

这是我的活动................

MainActivity.java

package com.infobulls.touchdemo;


public class MainActivity extends Activity implements OnTouchListener 
{

    ImageButton imageButton;
    protected int _splashTime = 3000;

    public boolean isVideoCaptured = false;
    public boolean isImageCaptured = true;

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

        imageButton = (ImageButton) findViewById(R.id.imageButton);
        imageButton.setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        isImageCaptured = true;
        isVideoCaptured = true;

        int action = event.getAction();
        if(action == MotionEvent.ACTION_DOWN)
        {   
            try 
            {   
                Timer timer = new Timer();
                timer.schedule(new TimerTask() 
                {
                    @Override
                    public void run()
                    {                           
                         show();
                    }
                }, _splashTime);
            } 
            catch (Exception e) 
            {
                System.out.println(e + "splash screen carsh");
            }


        }
        else if(action == MotionEvent.ACTION_UP)
        {
            isVideoCaptured = false;
            show();
            if(isImageCaptured)
            {

                Intent image =new Intent(MainActivity.this,ImageNextClass.class);
                startActivity(image);
                Toast.makeText(getApplicationContext(), "inside Up Event", Toast.LENGTH_SHORT).show();

            }

        }

        return true;
    }

    public void show()
    {
        if(isVideoCaptured)
        {
            isImageCaptured=false;
            System.out.println("inside show method=============");
            Intent next =new Intent(MainActivity.this,NextClass.class);
            startActivity(next);                
        }
    }
}

ImageNextClass.java

package com.infobulls.touchdemo;


public class ImageNextClass extends Activity
{

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

}

NextClass.java

package com.infobulls.touchdemo;


public class NextClass extends Activity
{

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

}

你好朋友,               我正在尝试运行此代码,但它没有正常工作,因为我只想在同一个按钮上的触摸事件上运行。但我无法运行此代码。 这里我没有使用onClickListener我只使用onTouchListener并执行单击并触摸onTouchListener上的两个操作并使用布尔变量进行管理如果你触摸按钮并保持...比3秒后next.java活动调用如果你是触摸和立即释放按钮而非触摸事件工作,如单击事件和imagenext.java活动调用。 在此先感谢

1 个答案:

答案 0 :(得分:0)

尝试使用onClickListener和setOnLongClickListener

imageButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

   Toast.makeText(mainActivity.this, "normal Press", Toast.LENGTH_LONG).show();

});

而不是

imageButton.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        Toast.makeText(mainActivity.this, "Long preess", Toast.LENGTH_LONG).show();

        return true;
    }
});