27

私はAndroidを初めて使用します。HelloWorldチュートリアルを実行し、何が起こっているのかについての基本的な考え方を持っています。私はT-MobilePulseのタッチスクリーンに特に興味があるので、始めに、画面にtocuhイベントの座標を書き込めるようにしたいので、ユーザーが座標5に触れたとしましょう。 、2-画面上のテキストビューにそれが表示されます。

現在、座標を書き込む予定のテキストビューを含むxmlファイルをロードするだけの簡単なプログラムがあります。

事前に感謝します。私はGoogleに助けを求め、stackoverflowを検索しましたが、見つけたものはすべて頭を悩ませたか、これには適していませんでした。乾杯。

4

4 に答える 4

44

この関数を使用できます:http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

あなたはおそらくそれをあなたのonCreateメソッドに大まかにこのように置くでしょう(今回テストされました):

アクティビティonCreateコード

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView textView = (TextView)findViewById(R.id.textView);
    // this is the view on which you will listen for touch events
    final View touchView = findViewById(R.id.touchView);
    touchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            textView.setText("Touch coordinates : " +
                String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
                return true;
        }
    });
}

レイアウトコード

<?xml version="1.0" encoding="utf-8"?>                                      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/touchView" 
    android:background="#f00" 
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/textView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, TestActivity"
    />
</LinearLayout>

編集:コードを試しましたが、実際にいくつかのエラーがありました。とにかく、ここで紹介するレイアウトでは、エミュレーターで動作します。何が悪いのかがわかるように、もっと多くのコード/コンテキストを提供できますか?

于 2010-05-30T16:15:11.437 に答える
3

この特定のテストでは、レイアウトの全領域からタッチイベントを取得したいようです。別のターゲットビューではなく、親ビューにタッチリスナーをアタッチしてみてください。

これはあまり一般的なアプローチではありません。ほとんどの場合、特定の子ビューでタッチイベントをリッスンする必要があります。レイアウトにタッチイベントを処理する他のビュー(ボタンなど)がある場合は、親ビューよりも優先されます。UIイベントの処理の詳細については、http://developer.android.com/guide/topics/ui/ui-events.htmlを参照してください。

レイアウト(touch_viewer.xml):

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parent" >
    <TextView android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />
</LinearLayout>

そしてあなたの活動では:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.touch_viewer);

    final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
    final TextView text = (TextView) findViewById(R.id.text);
    parent.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent ev) {
            text.setText("Touch at " + ev.getX() + ", " + ev.getY());
            return true;
        }
    });
}
于 2010-05-30T18:39:01.713 に答える
0

以下は、jetboyサンプルAndroidアプリのタッチスクリーンを使用して動作します。Joesコードは、1つの調整で機能し、背景を照らします。私が追加した唯一の行は

android:background="#0000"
android:layout_height="fill_parent"   
android:layout_height="fill_parent" 

ジョー(上記)に感謝します。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView textView = (TextView)findViewById(R.id.textView);
    // this is the view on which you will listen for touch events
    final View touchView = findViewById(R.id.touchView);
    touchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            textView.setText("Touch coordinates : " +
                String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
                return true;
        }
    });
}

レイアウトコード

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.android.samples.jetboy.JetBoyView android:id="@+id/JetBoyView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
        <TextView  
                android:id="@+id/touchView" 
                android:background="#0000" 
                android:layout_weight="1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
        />
        <TextView  
                android:id="@+id/textView" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="Hello World, TestActivity"
        />
</FrameLayout>
于 2010-12-29T03:28:38.583 に答える
0

エラーのない修正バージョン:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.textView);
        // this is the view on which you will listen for touch events
        final View touchView = findViewById(R.id.touchView);
        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                textView.setText("Touch coordinates : "
                        + String.valueOf(event.getX()) + "x"
                        + String.valueOf(event.getY()));
                return true;
            }
        });
    }
    @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;
    }
}
于 2014-03-07T15:47:39.147 に答える