在主活动中触摸任何地方启动一个活动

3

我是一名新手安卓开发者。我创建了一个应用程序,其中包含两个活动 - 主活动和另一个名为New1的活动。 现在这是我需要做的。我希望当用户在主活动上触摸任何地方时启动第二个活动。我尝试使用onTouch()来实现这个目标,但它不起作用。 以下是MainActivity.java代码:

package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.view.View.OnTouchListener;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnTouchListener {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onTouch(View v, MotionEvent event) {
      Intent intent = new Intent(this, New1.class);
      startActivity(intent);
        return true;    
    }
}

以下是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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="206dp"
        android:text="Touch Anywhere" />

</RelativeLayout>

我正在使用Eclipse,并以Android 4.0为目标。请帮忙!


尝试为RelativeLayout设置触摸监听器。 - Triode
4个回答

1

尝试使用此覆盖方法。当用户与屏幕交互时,将调用此方法。

 public void onUserInteraction() {
    super.onUserInteraction();
   // Call your intent here.
   };

您可以在这里阅读更多相关编程内容。


1
当您将侦听器附加到某物时,侦听器将起作用。我可以看到您已经实现了onTouchListener,但是还没有将该侦听器附加到任何内容上。通过findViewById获取相对布局的引用或获取活动的decoreview,并将侦听器附加到该视图上。
例如:
RelativeLayout rl = (RelativeLayout)findViewById(R.id. some id);
rl.setOnTouchListener(this);

1
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
     android:id="@+id/relativeLayout1" >

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="206dp"
    android:text="Touch Anywhere" />

</RelativeLayout>

然后在 onCreate 中设置 RelativeLayout 的触摸监听器,即:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
        layout.setOnTouchListener(this);
    }

0
我建议在你的xml布局父项中添加android:id="@+id/myParentLayout",然后在该视图上使用OnTouchListener。你可以在http://developer.android.com/reference/android/view/View.OnTouchListener.html阅读关于OnTouchListener的内容。这应该能完成工作。
.....
// In onCreate for Activity
  // Find your view , ex: if your parent is a LinearLayout
  LinearLayout mParentLayout = (LinearLayout) findViewById(R.id.myParentLayout);
  myParentLayout.setOnTouchListener(ParentTouchListener);


 } // end onCreate()

 OnTouchListener ParentTouchListener = new OnTouchListener(){
      @Override
      public boolean onTouch(View v, MotionEvent event){

         // Handle Actions if user presses down on screen
         if(event.getAction() == MotionEvent.ACTION_DOWN){

           // Do something

           // Action supported, screen was touched
           return true;
        }

         // default return when touch action is unsupported
         return false;
      }
 };

另外,MotionEvent的操作在http://developer.android.com/reference/android/view/MotionEvent.html上。


没错!Saif 和 Mukesh 也一样……感谢您的时间。 - Akashdeep Singh

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