Android - 在自定义视图的onTouch方法中向mainActivity发送信息

3
我有一个自定义视图(DrawFigures),其中有一个onTouch方法来执行一系列操作。有一个布局,其中有几个按钮、一个TextView和一个RelativeView来动态添加自定义视图。然后,在MainActivity.class中,当在此自定义视图中调用onTouch方法时,我需要接收DrawFigures中一些属性的值,以便在TextView组件中显示它们。
这是自定义视图的代码:
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;
}

我无法在MainActivity的TextView中显示value1,因为它在DrawFigures的onTouch方法中被改变了。如何解决这个问题?是在MainActivity中实现OnTouch方法吗?该怎么做?MainActivity.class看起来像这样:
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;
    }
}

为什么不将 DrawFigures 设为 MainActivity 的内部类,并将 value1 设为全局类成员? - Marcus
或者,将您希望在“MainActivity”中访问的值保存在“SharedPreference”中。 - Marcus
DrawFigures类是一个相当大的类,也被其他子类扩展,因此将其放在MainActivity中不太方便。关于SharedPreference,应该在哪里使用它?主要问题是在调用DrawFigures的onTouch方法时如何使用value1的值。 - daalgi
3个回答

0

经过一段时间的尝试,我找到了一个解决方案。只需在MainActivity的onTouch方法中使用实例“fig”调用DrawFigures类中的onTouchEvent方法即可。

我想感谢那些花时间帮助我的人,谢谢!

以下是代码:

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

}


0
你可以在activity中实现onTouchListener,在调用父类的onTouchEvent后,通过getter更新值。代码示例如下:
@Override
public boolean onTouch(final View v, final MotionEvent event) {
    super.onTouchEvent(event)
    float value1 = fig.getUpdatedValues()
    return true;
}

记得在你的活动中为该视图分配OnTouchListener


"fig" 是 DrawFigures 的一个实例。这个类有一个 getter 方法来获取 value1 的值。但是我尝试了这段代码,它不起作用。 - daalgi
哎呀,错过了。编辑后的版本应该可以工作,基本上是浮点值1 = fig.getUpdatedValues()。因此,从自定义视图“fig”中获取每个MotionEvent.ACTION_MOVE更新的值1。 - harrane
还是不行。似乎DrawFigures的onTouch方法中的代码没有加载。 - daalgi
你在调用super吗?super.onTouchEvent(event) - harrane

0

您可以在非活动类中声明value1为静态变量,如下所示:

static float value1;

然后在您的Activity类中按照以下方式获取该值:

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

或者,由于您的值是私有的,您可以使用settergetter方法来使用DrawFigures对象设置和检索该值,然后在整个应用程序中使用。

如果您希望该值持久化,可以使用SharedPreferences


当DrawFigure类的onTouch方法被调用时,我需要获取value1的值。你写的代码应该放在Activity类的哪里?我尝试在Activity类中使用onTouch方法,但没有成功。 - daalgi
确保在调用Activity类中的value1之前,Drawfigures类已经分配了一个值。 - Ojonugwa Jude Ochalifu
仍然没有起作用。现在看来,DrawFigures的onTouch方法内的代码没有加载。我刚刚编辑了我的问题,添加了当按钮被按下时调用的方法,它可能会有影响。 - daalgi
你初始化了你的按钮吗? - Ojonugwa Jude Ochalifu
好的,我不知道哥们儿,希望我知道问题所在。但你可以尝试另一种选项——SharedPreferences。如果你能给我一些时间,我会给你一个样例让你试试。 - Ojonugwa Jude Ochalifu
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接