onTouchListener - 2. Finger und "UpEvent" funktioniert nicht

Linux4ever

Linux4ever

Fortgeschrittenes Mitglied
27
Guten Tag liebe Community,

wisst ihr was ich hier falsch mache? Ich möchte das auch beim zweiten Finger ein Toast angezeigt wird, ebenso beim UpEvent.

Code:

Code:
package de.fpprogs.stk.lenkrad;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class STKLenkradActivity extends Activity implements OnTouchListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textViewsDefinieren();
        
    }

    public void textViewsDefinieren() 
    {
        TextView text_fire = (TextView) findViewById(R.id.text_fire);
        text_fire.setOnTouchListener(this);
        
        TextView text_lookback = (TextView) findViewById(R.id.text_lookback);
        text_lookback.setOnTouchListener(this);
        
        TextView text_nitro = (TextView) findViewById(R.id.text_nitro);
        text_nitro.setOnTouchListener(this);
        
        TextView text_slide = (TextView) findViewById(R.id.text_slide);
        text_slide.setOnTouchListener(this);
        
        TextView text_speedup = (TextView) findViewById(R.id.text_speedup);
        text_speedup.setOnTouchListener(this);
        
        TextView text_speeddown = (TextView) findViewById(R.id.text_speeddown);
        text_speeddown.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        {
            tasteErmittelnDown(v);
        }
        
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        {
            tasteErmittelnUp(v);
        }
        return false;
    }

    public void tasteErmittelnUp(View v) 
    {
        switch(v.getId())
        {
            case R.id.text_fire:
                sendToServerUp("VK_R");
                break;
        
            case R.id.text_lookback:
                sendToServerUp("VK_F");
                break;
        
            case R.id.text_nitro:
                sendToServerUp("VK_T");
                break;
        
            case R.id.text_slide:
                sendToServerUp("VK_G");
                break;
        
            case R.id.text_speedup:
                sendToServerUp("VK_Z");
                break;
        
            case R.id.text_speeddown:
                sendToServerUp("VK_H");
                break;
        }
    }

    public void tasteErmittelnDown(View v) 
    {
        switch(v.getId())
        {
            case R.id.text_fire:
                sendToServerDown("VK_R");
                break;
        
            case R.id.text_lookback:
                sendToServerDown("VK_F");
                break;
        
            case R.id.text_nitro:
                sendToServerDown("VK_T");
                break;
        
            case R.id.text_slide:
                sendToServerDown("VK_G");
                break;
        
            case R.id.text_speedup:
                sendToServerDown("VK_Z");
                break;
        
            case R.id.text_speeddown:
                sendToServerDown("VK_H");
                break;
        }
    }

    private void sendToServerUp(String taste) 
    {
        Toast.makeText(this, taste + " wurde losgelassen", Toast.LENGTH_LONG).show();
    }

    private void sendToServerDown(String taste) 
    {
        Toast.makeText(this, taste + " wurde berührt", Toast.LENGTH_LONG).show();
    }
}

Wenn ihr möchtet, kann ich noch das Layout posten, ebenso das Manifest. Ich progge mit Eclipse.

Viele Grüße, L4e
 
Du kannst keine zwei unterschiedliche Buttons gleichzeitig drucken.
Oder habe ich das jetzt beim überfliegen falsch gelesen?
 
  • Danke
Reaktionen: Linux4ever
Hmmm... Genau das will ich ja. Ich habe dies mit 6 TextViews realisiert, die auf Berührung reagieren.

Layout:

Code:
<?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" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/text_speedup"
            android:layout_width="160dp"
            android:layout_height="155dp"
            android:layout_alignBottom="@+id/text_nitro"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="@string/speed_up"
            android:textSize="30dip" />

        <TextView
            android:id="@+id/text_fire"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_above="@+id/text_lookback"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="@string/fire"
            android:textSize="30dip" />

        <TextView
            android:id="@+id/text_nitro"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/text_fire"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/TextView1"
            android:layout_toRightOf="@+id/text_fire"
            android:gravity="center"
            android:text="@string/nitro"
            android:textSize="30dip" />

        <TextView
            android:id="@+id/text_lookback"
            android:layout_width="wrap_content"
            android:layout_height="130dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/text_fire"
            android:gravity="center"
            android:text="@string/look_back"
            android:textSize="30dip" />

        <TextView
            android:id="@+id/text_slide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/text_nitro"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/text_nitro"
            android:layout_alignTop="@+id/text_lookback"
            android:gravity="center"
            android:text="@string/slide"
            android:textSize="30dip" />

        <TextView
            android:id="@+id/text_speeddown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/text_speedup"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/text_slide"
            android:gravity="center"
            android:text="@string/speed_down"
            android:textSize="30dip" />

    </RelativeLayout>

</LinearLayout>

Kennst du eine Verbesserung, wie ich Multitouch "machen" kann? >Es reicht mit 2 Fingern. Mehr kann meins auch nicht ;)

Grüße und Danke für deine Hilfe, L4e
 
Bin ich blöd... Da fehlte natürlich ein "return;"...

Code:
Code:
package de.fpprogs.stk.lenkrad;


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class STKLenkradActivity extends Activity implements OnTouchListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textViewsDefinieren();
        
    }

    public void textViewsDefinieren() 
    {
        TextView text_fire = (TextView) findViewById(R.id.text_fire);
        text_fire.setOnTouchListener(this);
        
        TextView text_lookback = (TextView) findViewById(R.id.text_lookback);
        text_lookback.setOnTouchListener(this);
        
        TextView text_nitro = (TextView) findViewById(R.id.text_nitro);
        text_nitro.setOnTouchListener(this);
        
        TextView text_slide = (TextView) findViewById(R.id.text_slide);
        text_slide.setOnTouchListener(this);
        
        TextView text_speedup = (TextView) findViewById(R.id.text_speedup);
        text_speedup.setOnTouchListener(this);
        
        TextView text_speeddown = (TextView) findViewById(R.id.text_speeddown);
        text_speeddown.setOnTouchListener(this);
        
        return;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        {
            try 
            {
                tasteErmittelnDown(v);
                return true;
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(this, "Fehler" + e, Toast.LENGTH_LONG).show();
            }
        }
        
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        {
            try 
            {
                tasteErmittelnUp(v);
                return true;
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(this, "Fehler" + e, Toast.LENGTH_LONG).show();
            }
        }
        return false;
    }

    public void tasteErmittelnUp(View v) throws IOException 
    {
        switch(v.getId())
        {
            case R.id.text_fire:
                sendToServerUp("VK_R");
                break;
        
            case R.id.text_lookback:
                sendToServerUp("VK_F");
                break;
        
            case R.id.text_nitro:
                sendToServerUp("VK_T");
                break;
        
            case R.id.text_slide:
                sendToServerUp("VK_G");
                break;
        
            case R.id.text_speedup:
                sendToServerUp("VK_Z");
                break;
        
            case R.id.text_speeddown:
                sendToServerUp("VK_H");
                break;
        }
        return;
    }

    public void tasteErmittelnDown(View v) throws IOException 
    {
        switch(v.getId())
        {
            case R.id.text_fire:
                sendToServerDown("VK_R");
                break;
        
            case R.id.text_lookback:
                sendToServerDown("VK_F");
                break;
        
            case R.id.text_nitro:
                sendToServerDown("VK_T");
                break;
        
            case R.id.text_slide:
                sendToServerDown("VK_G");
                break;
        
            case R.id.text_speedup:
                sendToServerDown("VK_Z");
                break;
        
            case R.id.text_speeddown:
                sendToServerDown("VK_H");
                break;
        }
        return;
    }

    private void sendToServerUp(String taste) throws IOException
    {
        Toast.makeText(this, taste + " wurde losgelassen", Toast.LENGTH_LONG).show();
        
        try
        {
            Socket s = new Socket("192.168.2.27",1234);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            out.write(taste);
            out.newLine();
            out.flush();
            out.close();
            s.close();
        }
        catch (IOException e)
        {
            Toast.makeText(this, "Fehler: " + e, Toast.LENGTH_LONG).show();
        }
        return;
    }

    private void sendToServerDown(String taste) throws IOException 
    {
        Toast.makeText(this, taste + " wurde berührt", Toast.LENGTH_LONG).show();
        
        try
        {
            Socket s = new Socket("192.168.2.27",1234);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            out.write(taste);
            out.flush();
            out.close();
            s.close();
        }
        catch (IOException e)
        {
            Toast.makeText(this, "Fehler: " + e, Toast.LENGTH_LONG).show();
        }
        return;
    }
}
Danke trotzdem für deine Bemühungen the_alien!!!:thumbsup:


EDIT: Wie kann man das Thema als gelöst markieren???
 
Zuletzt bearbeitet:
Man kann zwei buttons gleichzeitig drücken?
 
  • Danke
Reaktionen: Linux4ever
Mist:blushing:

Da war ich wohl zu voreilig...

Das funktioniert immer noch nicht. Aber das Lösen und Drücken funzt.

Kennst du eine Möglichkeit, wie man dies realisieren kann??

Danke für deine Hilfe!
 

Ähnliche Themen

SaniMatthias
Antworten
19
Aufrufe
960
swa00
swa00
M
  • MikelKatzengreis
Antworten
5
Aufrufe
131
swa00
swa00
O
Antworten
15
Aufrufe
2.972
ORHUX
O
Zurück
Oben Unten