如何在Android中处理同一按钮的onClick和onTouch事件

3
This is my all xml files..........(这是我所有的xml文件..........) activity_main.xml(主活动.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>

下面是我的Activity................

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);
    }

}

大家好, 我正在尝试运行这段代码,但是它没有正常工作,因为我只想在同一个按钮的触摸事件上运行它。但是我无法运行这段代码。 在这里,我没有使用onClickListner,我只使用onTouchListener,并在onTouchListener上执行click和touch两个操作,并使用布尔变量进行管理,如果您在按钮上触摸并保持不动,那么3秒钟后会调用next.java活动,如果您触摸并立即释放按钮,则触摸事件将像单击事件一样工作,并调用imagenext.java活动。 提前致谢

1个回答

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;
    }
});

嗨Dhwanik,你是对的,但是在安卓上如何同时保持点击和触摸监听器在相同的按钮上呢?当捕获图像和视频时,立即进行图像捕获,而当捕获视频时,视频捕获将在3秒后开始,并且进度条也会开始运行。当视频停止时,进度条也会停止,所以我正在使用onTouchListener。 - nikhil2505

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接