我是新来的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作为目标。请帮忙!
尝试将触摸侦听器的RelativeLayout – Triode