私の問題は、画面のどこにいても、どんなタッチでも受信できるようにしたいということです。そのために私が考えたのは、メインの「RelativeLayout」を使用してレイアウトを作成することです。アクティビティは「onTouchListener」を実装します。これは私が持っているレイアウトです:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" /></RelativeLayout>
これは私のアクティビティのコードです:
public class MainActivity extends Activity implements OnTouchListener{
private Context context = null;
private ImageView imagen = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
imagen = (ImageView)findViewById(R.id.imageView1);
imagen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
RelativeLayout relative = (RelativeLayout)findViewById(R.id.relative);
relative.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, "Touch!!", Toast.LENGTH_SHORT).show();
return false;
}
重要なのは、画像をタッチするとトーストが表示されないということです(画面上のどこにタッチしてもトーストを表示したいのですが)。
アクティビティの画面に触れたイベントをどのように受け取ることができるかを誰もが知っています(どこにいても??)。
編集:私が欲しいのはImageViewのリスナーでもありません。私が欲しいのは、画面が何であれ、ユーザーが画面に触れたというイベントを受信する方法があるかどうかを知ることです。
どうもありがとう!