繁体   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