gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:03:55 25 4
gpt4 key购买 nike

这是我所有的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>

这是我的 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);
}

}

你好 friend 们, 我正在尝试运行此代码,但它无法正常工作,因为我只想在同一按钮上的触摸事件上运行。但我无法运行这段代码。在这里我没有使用 onClickListener 我只使用 onTouchListener 并在 onTouchListener 上执行点击和触摸操作,如果你触摸按钮并按住则使用 bool 变量进行管理.. 比 3 秒后 next.java Activity 调用如果你触摸和在按钮上立即释放比触摸事件更像点击事件和调用的 imagenext.java Activity 。提前致谢

最佳答案

尝试使用 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;
}
});

关于android - 如何在android中处理同一个按钮上的onClick和onTouch事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com