简体   繁体   中英

Android - send information to the mainActivity from the onTouch method in a Custom View

I have a custom view (DrawFigures) which has an onTouch method to perform a series of actions. There's a layout that has a few buttons, a TextView and a RelativeView to add the custom view dynamically. Then, in the MainActivity.class, I need to receive the value of some attributes in the DrawFigures when the onTouch method is called in this custom view, in order to show them in TextView components.

Here is the code of the custom view:

public abstract class DrawFigures extends View{
    private float value1;
    public boolean onTouch(Motion Event){
        int action = event.getAction();
        if(action==MotionEvent.ACTION_MOVE){
            // ... methods that provoke that value1 changes
        }else if(action==MotionEvent.ACTION_DOWN){
            // ...        
        }else if(action==MotionEvent.ACTION_UP){
            // ...
        }
    }
    // ...
    return true;
}

I haven't been able to receive the value1 to show it in the TextView of the MainActivity while it's changing because of the onTouch method of the DrawFigures. How could be this problem solved? Implement an OnTouch method in the MianActivity? How to do it? The MainActivity.class looks like this:

public class MainActivity extends Activity implements View.OnTouchListener{
    DrawFigures fig;
    TextView txtInfo;
    // ...
    public void btnSecRec(View v){
        int id = v.getId();
        if (id == R.id.btnSecRec){
            RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
            parent.removeAllViews();
            fig = new DrawRectangleWithDimensions(this);
            fig.setOnTouchListener(this);
            fig.setFigure(10, 40);
            parent.addView(fig);
        }
    }
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_MOVE) {

            if (fig.touching == true) {
                String value = Float.toString(fig.getChangingDimensionValue());

                txtInfo.setText(value);
            }
        }
        return true;
    }
}

You could implement the onTouchListener in the activity and after the calling the super class' onTouchEvent, you update the values through a getter. something like this:

@Override
public boolean onTouch(final View v, final MotionEvent event) {
    super.onTouchEvent(event)
    float value1 = fig.getUpdatedValues()
    return true;
}

Remember to also assign the OnTouchListener for that view in your activity

You could declare value1 as static in your non-activity class like this.

static float value1;

then get the value in your Activity class like this:

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    String value = Double.toString(DrawFigures.value1);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());

            txtInfoc.setText(value);
        }
    }

Or since your value is private, you can use setter and getter methods to set and retrieve the value with an object of DrawFigures which you can then use across your app.

If you want the value to be persisted, you can use SharedPreferences

After trying for a while, I found out a solution. Simply call the onTouchEvent method in the DrawFigures class using the instance "fig" inside the onTouch method of the MainActivity.

I'd like to thank the people who spent time trying to help, thanks!

Here's the code:

public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
    int id = v.getId();
    if (id == R.id.btnSecRec){
        RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
        parent.removeAllViews();
        fig = new DrawRectangleWithDimensions(this);
        fig.setOnTouchListener(this);
        fig.setFigure(10, 40);
        parent.addView(fig);
    }
}
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    fig.onTouchEvent(event);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());
            txtInfo.setText(value);
        }
    }
    return true;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM