如何让Android方法反复运作?

时间:2015-01-24 17:58:59

标签: java android eclipse

我设计了一个应用程序,在点击一次时显示文本,并在长按和双击时以不同方式显示。但是,我观察到这些方法被调用一次。这是一旦我长按或点击一次或双击,相应的方法被调用,然后在随后点击或按下什么都不做。可以做些什么来使应用程序不仅仅工作一次?

package com.example.hello;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnTouchListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity implements OnTouchListener {

    private TextView shownamecenter; 
    private TextView shownamecustom; 
    private RelativeLayout myimage; 
    int x, y;



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

        shownamecenter = (TextView)findViewById(R.id.shownamecenter);
        shownamecustom = (TextView)findViewById(R.id.shownamecustom);
        myimage = (RelativeLayout)findViewById(R.id.myimage);
        shownamecenter.setText("");
        shownamecustom.setText("");
        myimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showNameOnSingleTap();
                return;

            }
        });; 

        myimage.setOnTouchListener(this); //{



        myimage.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                showNameInCustomPosition(shownamecustom, x, y);// TODO Auto-generated method stub
                return true;
            }
        });


        return;
    }





        private void showNameOnSingleTap() {
            Timer countdown = new Timer(false);
            countdown.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            shownamecenter.setVisibility(View.INVISIBLE);
                        }
                    });
                }
            },3000);

            shownamecustom.setText("");
            shownamecenter.setText("My Text");
            shownamecenter.setTextColor(0xff00ff00);
            RelativeLayout.LayoutParams layoutparameters = (RelativeLayout.LayoutParams)shownamecenter.getLayoutParams();
            layoutparameters.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
            shownamecenter.setLayoutParams(layoutparameters);



            findViewById(R.id.myimage).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    shownamecenter.setText("");

                    return;

                }
            });
            return;




        }

        private void showNameInCustomPosition(TextView customview, int x, int y) {
            Timer countdown = new Timer(false);
            countdown.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            shownamecustom.setVisibility(View.INVISIBLE);
                        }
                    });
                }
            },3000);

            RelativeLayout.LayoutParams relout = (RelativeLayout.LayoutParams) customview.getLayoutParams();
            relout.leftMargin = x-50;
            relout.topMargin = y-50;
            customview.setLayoutParams(relout);
            shownamecenter.setText("");
            shownamecustom.setText("My Text");
            shownamecustom.setTextColor(0xff00ff00);

            class GestureListener extends GestureDetector.SimpleOnGestureListener {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    shownamecustom.setText("");
                    showNameOnSingleTap();
                    return true;
                }

            }
            return;

        }











    //}


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        x = (int)event.getRawX();
        y = (int)event.getRawY();

        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

问题是因为你正在重新定义点击图像时发生的事情,这不是一个非常好的做法,因为它很难跟踪应该发生的事情。一个视图只能有一个每种类型的监听器,所以当你这样做时

private void showNameOnSingleTap() {
    //... 
    findViewById(R.id.myimage).setOnClickListener   (new View.OnClickListener() {   
        @Override public void onClick(View v) {             
            shownamecenter.setText(""); return; 
        } 
    });
} 

您正在更改每次单击图像后发生的情况。而不是在点击时调用方法showNameOnSingleTap(),而只是设置文本而不是调用方法。不要重新分配onClickListener,只需保留一个布尔变量来确定你想要做什么。

boolean textIsVisible = false;
private void showNameOnSingleTap() {
    if(textIsVisible) {
        //hide text
        textIsVisible = false;
        shownamecenter.setText(View.INVISIBLE);
    } 
    else {
        Timer countdown = new Timer(false);
        countdown.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        shownamecenter.setVisibility(View.INVISIBLE);
                        textIsVisible = false;
                    }
                });
            }
        },3000);
        //show text
        textIsVisible = true;
    } 
}