2014-03-03 21 views
2

enter image description here我在我的Android應用程序中有一個字符串生成器函數。我希望每次有「輕敲」或「觸摸」時調用字符串生成器函數。字符串生成器函數將更改文本標籤中的字符串。我沒有任何類型的錯誤,但是當我在Android模擬器和物理設備上測試應用程序時,文本標籤中的字符串沒有更改,這意味着字符串生成器函數未被調用。如何正確使用Android OnTouchListener和OnTouch功能?

這裏是我的代碼如下所示:

public class MainActivity extends Activity{ 

    //member variables 
    private ExcuseGenerator mExcuseGenerator = new ExcuseGenerator(); 
    private ImageView mMrExcuse; 
    private TextView mExcuse; 
    private RelativeLayout rl; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mMrExcuse = (ImageView) findViewById(R.id.imageView1); 
     mExcuse = (TextView) findViewById(R.id.textView1); 
     rl = (RelativeLayout) findViewById(R.id.rl1); 

     rl.setOnTouchListener(
       new View.OnTouchListener(){ 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         if(event.getAction() == MotionEvent.ACTION_UP){ 

          //here is the string generator function 
          excuseGenerator(); 
          return true; 
         } 
         return false; 
        } 
       } 
       ); 
    } 

注:我想水龍頭/倒是可以在屏幕上的任何地方檢測到,這就是爲什麼我使用了OnTouchListener相對佈局。

編輯:添加屏幕截圖顯示XML文件。

編輯2:這裏是我的excuseGenerator功能:

private void excuseGenerator(){ 
     String excuse = mExcuseGenerator.getExcuse(); 
     mExcuse.setText(excuse); //mExcuse is a text label. 
    } 

這裏是從類mExcuseGenerator的getExcuse功能:

/* 
    * I want to create a "real random experience". 
    * This function will have 2 lists. Every time an excuse is generated, 
    * it is deleted from its original array and then put into a temporary array 
    * so that the same excuses are not to be seen over and over again. 
    * when the original array is emptied, it will be reassigned the set of excuses 
    * and the temporary array will be emptied again. 
    */ 
    public String getExcuse(){ 

     if(stringArray.isEmpty()){ 
      stringArray = Arrays.asList(mExcuseList); 
      tempHolder = null; 
     } 
     String excuse; 
     Random randomGenerator = new Random(); 

     // get the position of the element from the array 
     int randomElement = randomGenerator.nextInt(stringArray.size()); 
     excuse = stringArray.get(randomElement); 

     tempHolder.add(stringArray.get(randomElement)); 
     stringArray.remove(stringArray.get(randomElement)); 



     //return the element 
     return excuse; 
    } 
+0

是您的RelativeLayout表示根佈局? – Pankaj

+0

我不知道根佈局是什麼。你指的是XML嗎? –

+0

是的,我在談論XML。 – Pankaj

回答

5

如果要啓用touchListener在屏幕上的任何位置,然後使用方法OnTouchEvent(MotionEvent event)像這個。

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    switch(event.getAction() & MotionEvent.ACTION_MASK) 
{ 
    case MotionEvent.ACTION_DOWN : //Do something 
            break; 
    case MotionEvent.ACTION_UP : //Do something 
            break; 
    } 
    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; 
} 

現在,您不必爲特定視圖檢測touchEvents。

+0

我在switch語句開始的整個行下出現錯誤。 –

+0

現在我已經編輯了我的答案,正在使用&&而不是&開關的情況下,可能會輸入錯誤。 – Pankaj

+0

靠近。該應用程序不再崩潰,也沒有錯誤。我有ACTION_UP的excuseGenerator()函數..但仍然文本標籤不會更改字符串(這是excuseGenerator函數應該做的)。 –

0

在佈局的XML添加

android:clickable="true" 

你的RelativeLayout。

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/clicks" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="true" 
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:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

</RelativeLayout> 

,並在Java代碼中, MainActivity.java

package com.test.testing; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      RelativeLayout click = (RelativeLayout) findViewById(R.id.clicks); 

      click.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
          Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show(); 
        } 
      }); 
    } 

    @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; 
    } 

} 
+0

我在那裏添加了它。該應用程序仍在崩潰。 –

+0

刪除'if(event.getAction()== MotionEvent.ACTION_UP)',只是做你想要的東西 –

+0

它仍然崩潰。 –