簡體   English   中英

如何正確使用Android OnTouchListener和OnTouch函數?

[英]How to properly use the Android OnTouchListener and OnTouch function?

在此處輸入圖片說明 我的Android應用程序中有一個字符串生成器函數。 我希望每次有“ tap”或“ touch”調用字符串生成器函數。 然后,字符串生成器功能將更改文本標簽中的字符串。 我沒有任何類型的錯誤,但是當我在Android模擬器和物理設備上測試應用程序時,文本標簽中的字符串都沒有改變,這意味着未調用字符串生成器函數。

這是我的代碼:

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

如果要在屏幕上的任何位置啟用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。

請在發生ACTION_DOWN( http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN )事件時嘗試返回true。 這是第一次觸發的事件,如果您在這種情況下返回false,那么如果我沒記錯的話,不會觸發所有其他事件。

android:clickable="true"

到您的Layout XML中的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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM