Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从自定义视图处理事件以在android中启动活动_Android_Android Layout_Android Intent - Fatal编程技术网

如何从自定义视图处理事件以在android中启动活动

如何从自定义视图处理事件以在android中启动活动,android,android-layout,android-intent,Android,Android Layout,Android Intent,我已经准备了一个自定义视图,它被添加到一个“活动”中。现在,对于将在该视图上发生的事件,我希望更改“活动”的内容。由于自定义视图类和“活动”类不同,我无法处理它。请帮我解决此问题。如果您需要更新任何内容,请向我表示感谢。在Main_活动(包含视图)上,您应该在自定义视图中创建一个Main_活动变量,如下所示: private Main_activity activity; this.activity.update(); 然后你可以调用主活动中的任何函数update(),如下所示: priva

我已经准备了一个自定义视图,它被添加到一个“活动”中。现在,对于将在该视图上发生的事件,我希望更改“活动”的内容。由于自定义视图类和“活动”类不同,我无法处理它。请帮我解决此问题。如果您需要更新任何内容,请向我表示感谢。

在Main_活动(包含视图)上,您应该在自定义视图中创建一个Main_活动变量,如下所示:

private Main_activity activity;
this.activity.update();
然后你可以调用主活动中的任何函数update(),如下所示:

private Main_activity activity;
this.activity.update();

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

我知道在互联网上/自始至终都有很多类似的帖子,但这篇文章简单而完整

你的活动课:

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

因此,与我在“活动”中展示祝酒词不同,你可以用你自己的方式来做——隐藏一个视图,展示另一个视图,等等。

我认为你需要的是定义你自己的事件侦听器。请看这里:您是指1个活动(其中包含视图)还是指多个活动?我指的是1个活动,其中包含视图。这是不允许的,因为我想启动已添加视图的活动。对不起,但是重新启动您自己的活动而不是更新它在我看来是一个可怕的解决方案…是的,Rvdk我也只想更新活动是否有任何从自定义视图更新的解决方案?@user2459539您希望在您的活动(包含自定义视图的活动)中更新什么?实际上,我想使用两个自定义视图,其中一个视图一次只有一个在“活动”上&在该自定义视图的某些事件检测上,我想删除该自定义视图并添加另一个“活动”自定义视图。