2016-09-17 67 views
0

所以我已經嘗試了這一個教程,告訴手勢如何工作,我做了一個簡單的活動,檢測手勢並生成一個敬酒。現在我正在處理我的項目,並使我的主頁具有簡單的相對佈局和圖像視圖。手勢工作也在那裏找到。現在我做了一個新的幫助頁面,並盡我所能,但手勢不適合我。是因爲佈局嗎?因爲這個包含多個相對和線性佈局。 以下是我的幫助活動。我想在LongPress上打開語音識別(我正在嘗試在這裏敬酒,但它不工作) 任何幫助,將不勝感激。我可能錯過了一些愚蠢的手勢檢測不能在佈局上工作

package com.example.dell.optasia; 

import android.app.ActivityManager; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.media.MediaPlayer; 
import android.speech.tts.TextToSpeech; 
import android.support.v4.view.GestureDetectorCompat; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ViewSwitcher; 
import static android.view.GestureDetector.*; 
import org.w3c.dom.Text; 

import java.util.List; 

public class Help extends AppCompatActivity implements 
     OnGestureListener, OnDoubleTapListener { 
    private GestureDetectorCompat GestureDetect; 
    public MediaPlayer mp1; 
    TextView textView; 
    TextView answer5; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_help); 
     mp1 = MediaPlayer.create(Help.this, R.raw.help); 
     mp1.start(); 
     ActionBar ab = getSupportActionBar(); //Actionbar 
     ab.setLogo(R.mipmap.iconlaunch); 
     ab.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // set your desired color 

     ab.setDisplayUseLogoEnabled(true); //shows logo 
     ab.setDisplayShowHomeEnabled(true); //shows home page 
     TextView textView= (TextView) findViewById(R.id.textView); 

     GestureDetect = new GestureDetectorCompat(this, this); 
     GestureDetect.setOnDoubleTapListener(this); 

     TextView txtview = (TextView) findViewById(R.id.question1); 
     final TextView answer1 = (TextView) findViewById(R.id.answer1); 

       txtview.setOnClickListener(new View.OnClickListener() 
       { 
        @Override 
        public void onClick(View v) { 
         if (answer1.getVisibility() == View.VISIBLE) 
          answer1.setVisibility(View.GONE); 
         else 
         answer1.setVisibility(View.VISIBLE); 
        } 

       }); 

     TextView txtview1 = (TextView) findViewById(R.id.question2); 
     final TextView answer2 = (TextView) findViewById(R.id.answer2); 
       txtview1.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v){ 
         if (answer2.getVisibility() == View.VISIBLE) 
          answer2.setVisibility(View.GONE); 
         else 
          answer2.setVisibility(View.VISIBLE); 
         } 
       }); 
     TextView txtview2 = (TextView) findViewById(R.id.question3); 
     final TextView answer3 = (TextView) findViewById(R.id.answer3); 
       txtview2.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (answer3.getVisibility() == View.VISIBLE) 
          answer3.setVisibility(View.GONE); 
         else 
         answer3.setVisibility(View.VISIBLE); 
        } 
       }); 
     TextView txtview3 = (TextView) findViewById(R.id.question4); 
     final TextView answer4 = (TextView) findViewById(R.id.answer4); 

     txtview3.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (answer4.getVisibility() == View.VISIBLE) 
          answer4.setVisibility(View.GONE); 
         else 
         answer4.setVisibility(View.VISIBLE); 
        } 
       }); 

     TextView txtview4 = (TextView) findViewById(R.id.question5); 
     final TextView answer5 = (TextView) findViewById(R.id.answer5); 
       txtview4.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (answer5.getVisibility() == View.VISIBLE) 
          answer5.setVisibility(View.GONE); 
         else 
         answer5.setVisibility(View.VISIBLE); 
        } 
       }); 



    } 

    //for the overflow actionbar 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.main_activity_menu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    //for the actionbar menu items 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case R.id.Play_ID: 
       mp1 = MediaPlayer.create(Help.this, R.raw.manual); 
       mp1.start(); 
       break; 
      case R.id.Help_ID: 
       startActivity(new Intent(Help.this, Help.class)); 
       break; 
      case R.id.About_ID: { 
       startActivity(new Intent(Help.this, LocationMain.class)); 
       return true; 
      } 
      case R.id.Contact_Us:{ 
       startActivity(new Intent(Help.this, ContactUs.class)); 
       return true; 
      } 
      case android.R.id.home:{ 
       startActivity(new Intent(Help.this, HomeScreen.class)); 
       return true; 
      } 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
     return false; 
    } 
    //calls event for the gestures 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     GestureDetect.onTouchEvent(event); 
     return super.onTouchEvent(event); 
    } 
    @Override 
    public boolean onDoubleTap(MotionEvent motionEvent) { 
     return false; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) { 
     return false; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent motionEvent) { 
     if(mp1!=null) 
     { 
      mp1.stop(); 
     } return false; 
    } 

    @Override 
    public boolean onDown(MotionEvent motionEvent) { 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent motionEvent) { 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent motionEvent) { 
     return false; 
    } 

    @Override 
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent motionEvent) { 

     Toast.makeText(Help.this, "longpress!", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { 
     return false; 
    } 

} 

回答

0

您正在創建一個GestureDetector但你從來沒有「掛鉤起來」你查看。試着改變你的onCreate這樣的:

super.onCreate(savedInstanceState); 
View v = new RelativeLayout(this); 
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
setContentView(v); 
mDetector = new GestureDetectorCompat(this, this); 
mDetector.setOnDoubleTapListener(this); 
v.setOnTouchListener(new OnTouchListener(){ 
    public boolean onTouch(View v, MotionEvent me){ 
     return mDetector.onTouchEvent(me); 
    } 
}); 

,或者你可以試試這個

public boolean onTouchEvent(MotionEvent event) { 
    return gestureDetectorCompat.onTouchEvent(event) 
    } 
+0

羅是不是爲我工作。拋出nullPointerException。順便說一句,我沒有做這個「掛鉤」的東西在我的主頁上,但所有的手勢都很好。是因爲有多個相對的觀點? –

+0

我有edditted答案請檢查 – siddhesh

+0

我有這onTouchEvent已經在我的代碼。但它不在Oncreat方法中 –