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活动。 提前致谢