【问题标题】:Android - send information to the mainActivity from the onTouch method in a Custom ViewAndroid - 从自定义视图中的 onTouch 方法向 mainActivity 发送信息
【发布时间】:2015-02-20 11:31:47
【问题描述】:

我有一个自定义视图 (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;
}

由于 DrawFigures 的 onTouch 方法,我无法在 MainActivity 的 TextView 中接收到 value1 来显示它。这个问题怎么可能解决?在 MianActivity 中实现 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 设为全局类成员?
  • 或者,将您希望在MainActivity 中访问的值保留在SharedPreference
  • DrawFigures 类是一个很大的类,它还被其他子类扩展,所以放在MainActivity 中不太方便。关于 SharedPreference,在哪里使用它?主要问题是在调用 DrawFigures 的 onTouch 方法时使用了 value1。

标签: android view ontouch


【解决方案1】:

您可以在活动中实现 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 的一个实例。此类具有获取 value1 的 getter 方法。但是我尝试了这段代码,它不起作用。
  • 哎呀错过了。编辑后的版本应该可以工作,基本上是 float value1 = fig.getUpdatedValues()。因此,从自定义视图“fig”中,您将获得为每个 MotionEvent.ACTION_MOVE 更新的 value1
  • 还是不行。好像 DrawFigures 的 onTouch 方法里面的代码没有加载。
  • 你打电话给超级吗?? super.onTouchEvent(事件)
【解决方案2】:

您可以像这样在非活动类中将 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.class的onTouch方法时获取value1。你写的代码应该放在Activity.class的什么地方?我尝试在Activity类中使用onTouch方法没有成功。
  • 确保在 Drawfigures 类中的 value1 被分配一个值,然后再尝试在 Activity 类中使​​用它
  • 还是不行。现在似乎 DrawFigures 的 onTouch 方法中的代码没有加载。我刚刚编辑了我的问题,添加了按下按钮时调用的方法,它可能会产生影响。
  • 你初始化你的按钮了吗?
  • 按钮是用XML创建的,我没有在MainActivity中初始化。按钮(有四个)工作正常。
【解决方案3】:

尝试了一段时间后,我找到了解决方案。只需使用 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;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多