2013-12-11 77 views
3

我是新来的android编程。我创建了一个有两个活动的应用程序 - 主活动,然后是另一个名为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> 

我与日食工作,并采用了Android 4.0作为目标。请帮忙!

+0

尝试将触摸侦听器的RelativeLayout – Triode

回答

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

@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

谢谢先生!我的应用现在完美运行。 –

0

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

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

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

1

一位听众,当你附上监听到的东西会工作。正如我所看到的,你已经实现了onTouchListener,但是没有将该监听器附加到任何东西上。通过findViewById获取对你的relativelayout的引用,或者获取你的活动的decoreview,并将你的监听器附加到该视图。 例如:

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

谢谢!你是对的..它的工作原理.. –

0

我建议增加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行动是在http://developer.android.com/reference/android/view/MotionEvent.html

+0

这就对了!上面的Saif和Mukesh也是如此......谢谢你的时间 –