在OnTouch中访问更新变量时出现问题

时间:2010-05-29 01:33:36

标签: android ontouchlistener

我有一个OnTouch和一个setOnTouchListener来更新包含屏幕坐标信息的变量。问题是它似乎没有更新它们。在第78行,RGB.setText(测试);它永远不会从0.0变化。如果我要移动该行及其上方的行进入onTouch,它会更新。知道什么是错的吗?谢谢。

package com.evankimia.huskybus;

import com.test.huskybus.R;

import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView;

公共类HuskyBus扩展了Activity {

TextView RGB;
private CampusMap mCampusMap;

private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
float scrollByX = 0; //x amount to scroll by
float scrollByY = 0; //y amount to scroll by

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);



            setContentView(R.layout.main);

            RGB = (TextView) findViewById(R.id.coordBox);

            mCampusMap = (CampusMap) findViewById(R.id.map);
            mCampusMap.setOnTouchListener(new OnTouchListener() {

                                    @Override
                                    public boolean onTouch(View v, MotionEvent event) {
                                            // TODO Auto-generated method stub



                                            switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                            // Remember our initial down event location.

                            startX = event.getRawX();
                            startY = event.getRawY();
                            break;

                    case MotionEvent.ACTION_MOVE:
                            float x = event.getRawX();

                            float y = event.getRawY();
                            // Calculate move update. This will happen many times
                            // during the course of a single movement gesture.

                            scrollByX = x - startX; //move update x increment
                            scrollByY = y - startY; //move update y increment

                            startX = x; //reset initial values to latest
                            startY = y;



                            mCampusMap.invalidate();

                            break;
            }//end switch
                                            return false;
                                    }
                                    ;

            }); //end onDraw?

String test =“”+ scrollByX;                RGB.setText(测试);

    }

 }

1 个答案:

答案 0 :(得分:0)

如果你这样做,它应该有效:

package com.evankimia.huskybus;

import com.test.huskybus.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class HuskyBus extends Activity {
    TextView RGB;
    private CampusMap mCampusMap;

    private float startX = 0; // track x from one ACTION_MOVE to the next
    private float startY = 0; // track y from one ACTION_MOVE to the next
    float scrollByX = 0; // x amount to scroll by
    float scrollByY = 0; // y amount to scroll by

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        RGB = (TextView) findViewById(R.id.coordBox);

        mCampusMap = (CampusMap) findViewById(R.id.map);
        mCampusMap.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    // Remember our initial down event location.

                    startX = event.getRawX();
                    startY = event.getRawY();
                    break;

                case MotionEvent.ACTION_MOVE:
                    float x = event.getRawX();

                    float y = event.getRawY();
                    // Calculate move update. This will happen many times
                    // during the course of a single movement gesture.

                    scrollByX = x - startX; // move update x increment
                    scrollByY = y - startY; // move update y increment

                    startX = x; // reset initial values to latest
                    startY = y;

                    mCampusMap.invalidate();

                    break;
                }// end switch
                String test = "" + scrollByX;
                RGB.setText(test);
                return false;
            };

        }); // end onDraw?


    }
}

问题是你在执行TouchListener之外有RGB.setText(test);

顺便说一句,尝试在StackOverflow上发布之前整理代码。