【问题标题】:How to handle the event from custom view to start the activity in android如何处理来自自定义视图的事件以在 android 中启动活动
【发布时间】:2013-06-02 10:25:36
【问题描述】:

我已经准备了一个自定义视图并将它添加到一个活动中。现在根据我想更改活动的内容将在该视图上发生的事件。因为自定义视图类和活动类是不同的是我无法处理它。请帮我解决这个问题。谢谢。

【问题讨论】:

  • 我认为您需要定义自己的事件侦听器。看这里:stackoverflow.com/questions/8292712/…
  • 您是在谈论 1 个活动(其中包含视图)还是在谈论多个活动?
  • 我说的是 1 个有视图的活动

标签: android android-layout android-intent


【解决方案1】:

如果您必须更新 Main_activity(包含视图)上的任何内容,那么您应该在自定义视图中创建一个 Main_activity 变量,如下所示:

private Main_activity activity;

然后你可以像这样在主要活动中调用任何函数update():

this.activity.update();

【讨论】:

  • 这是不允许的,因为我想开始我已经添加了该视图的活动
  • 抱歉,重启你自己的 Activity 而不是更新它在我看来是一个糟糕的解决方案......
  • 是的 Rvdk 我也想只更新活动有没有从自定义视图更新的解决方案?
  • @user2459539 您究竟希望在您的活动中更新什么(包含您的自定义视图的活动)?
  • 实际上我想使用 2 个自定义视图,其中一次只有一个处于活动状态,并且在从该自定义视图进行某些事件检测时,我想删除该自定义视图并添加另一个活动的自定义视图。
【解决方案2】:

为了构建具有两个自定义视图和来自主活动的自定义侦听器的应用程序:

我知道互联网上/整个 SO 上都有很多类似的帖子,但这个简单而完整。

你的活动课:

public class MyActivity extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //initializing custom views
    MyCustomView1 myCustomView1 = new MyCustomView1(parameterList);
    MyCustomView2 myCustomView2 = new MyCustomView2(parameterList);

    //adding both custom views to the main activity
    mainView.addView(myCustomView1);
    mainView.addView(myCustomView1);

    //adding custom listener to the custom view 1
    myCustomView1.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 1
            Toast.makeText(MainActivity.this, "Touched custom view 1",
                    Toast.LENGTH_SHORT).show();
        }
    });

    //adding custom listener to the custom view 2
    myCustomView2.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 2
            Toast.makeText(MainActivity.this, "Touched custom view 2",
                    Toast.LENGTH_SHORT).show();
        }
    });
  }
}

你的 MyCustomView1 类:

public class MyCustomView1 extends LinearLayout{
  OnCustomEventListener myCustomEventListener;

  public MyCustomView1(ParameterList){
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 1");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView1.this.myCustomEventListener != null)
                MyCustomView1.this.myCustomEventListener.onEvent();
            return false;
        }
    });
  }

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

你的 MyCustomView2 类:

public class MyCustomView2 extends LinearLayout {
OnCustomEventListener myCustomEventListener;

public MyCustomView2(ParameterList) {
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 2");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView2.this.myCustomEventListener != null)
                MyCustomView2.this.myCustomEventListener.onEvent();
            return false;
        }
    });
}

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

您的监听器界面:

public interface OnCustomEventListener{
  //interface defines one method. Can be more and methods may have parameters
  public void onEvent();
}

因此,您可以按照自己的方式进行操作,而不是像我在活动中那样显示祝酒词 - 隐藏一个视图,显示另一个视图,等等。

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多