2013-12-11 75 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也是如此......謝謝你的時間 –