Problem accessing updated variables within OnTouch
I have an OnTouch and a setOnTouchListener that updates varibles which contain screen coord info. The problem is it doesnt seem to ever update them. On line 78, RGB.setText(test); it never changes from 0.0. If i were to move that line and the line above it into the onTouch it updates. any idea what is wrong? Thank you.
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 nex开发者_开发知识库t
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(test);
}
}
It should work if you do it this way:
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?
}
}
The problem is that you have the RGB.setText(test);
outside the execution of the TouchListener.
BTW, try to organize your code before posting here on StackOverflow.
精彩评论