【问题标题】:Launch an Activity on touching anywhere in Main Activity在主 Activity 中触摸任何位置时启动 Activity
【发布时间】:2013-12-11 14:03:37
【问题描述】:

我是安卓编程新手。我创建了一个包含两个活动的应用程序 - 主要活动,然后是另一个名为 New1。 现在这是我需要做的。我希望在用户触摸主 Activity 上的任何位置时启动第二个 Activity。我尝试使用 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设置触摸监听

标签: java android eclipse android-activity event-handling


【解决方案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>

然后将RelativeLayout 设置为onCreate 中的 louch 侦听器,即

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

【讨论】:

    【解决方案2】:

    尝试使用这种覆盖方法 如果用户与屏幕交互,则调用此方法。

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

    你可以阅读更多关于这个here

    【讨论】:

      【解决方案3】:

      当您将侦听器附加到某物时,该侦听器将起作用。正如我所看到的,您已经实现了 onTouchListener 但没有将该侦听器附加到任何东西。通过 findViewById 获取对您的 relativelayout 的引用或获取您的活动的 dereview 并将您的侦听器附加到该视图。 例如:

      RelativeLayout rl = (RelativeLayout)findViewById(R.id. some id);
      rl.setOnTouchListener(this);
      

      【讨论】:

        【解决方案4】:

        我建议将 android:id="@+id/myParentLayout" 添加到您的 xml 布局父项中,然后在该视图上使用 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 Actions 位于http://developer.android.com/reference/android/view/MotionEvent.html

        【讨论】:

        • 没错!上面的赛义夫和穆克什也是如此......谢谢你的时间
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2012-08-22
        • 1970-01-01
        • 2015-10-07
        相关资源
        最近更新 更多