如何正确终止自定义视图类的活动? [英] How to properly terminate an activity from a custom view class?

查看:81
本文介绍了如何正确终止自定义视图类的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于帖子.有一个自定义视图(扩展了 EditText ),如果用户按下END键,则该视图必须能够调用父活动的 finish()方法.

Similar to this post. Have a custom view (extends EditText) which must have the ability to call the finish() method of the parent activity if the user presses the END key.

我如何访问宿主活动的 activity 对象,以便从自定义视图类中调用其 finish()方法?

How can I access the activity object of the host activity in order to call its finish() method from within the custom view class?

public class SuppressInputEditText extends androidx.appcompat.widget.AppCompatEditText {

    public SuppressInputEditText(Context context) {
        super(context);
    }

    public SuppressInputEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SuppressInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode){
            case 6: //end key
                //todo: call finish() method of parent activity.
                break;
        }
        return true;
    }
    
}

我可以使用类的 getContext()方法来获得上下文,因为该方法继承自 view ,但我不知道该如何使用可以访问 finish()方法.任何帮助或指示,将不胜感激.

I can use the getContext() method of my class, available because it inherits from view, to get a context, but I don't know how to use that to access the finish() method. Any help or pointers would be greatly appreciated.

更新:寻找一种可以使类保持独立的解决方案.谢谢!

UPDATE: Looking for a solution that can keep the class independent. Thanks!

推荐答案

如果您知道主机,即自定义视图所显示的活动,则可以执行以下操作.

if you know the host i.e the activity where it's custom view is showing then you can do something like this.

(getContext() as? MainActivity)?.finish()

java

((MainActivity)getContext()).finish()

将其置于尝试并捕获状态

place this under try and catch

编辑:创建您的Host活动实现的接口,并将其作为侦听器传递给您的自定义视图,然后在需要时调用它.

Edit: Create an interface which your Host activity implements and pass this as a listener yo your custom view then whenever needed to call this.

例如.

interface CustomInputEditListener{
  public void onFinish();
}

在您的主持人"活动中实施此操作.

in your Host activity implement this.

MainActivity extends AppCompatActivity() implements CustomInputEditListener{

 //call this from onCreate()
  public void setHostListener(){
     suppressInputEditText.setHostEditListener(this);
  }

  @Override public void onFinish(){
      finish() ;
  }
}

在您的SuppressInputEditText类中创建这样的方法.

in your SuppressInputEditText class create a method like this.

public void setHostEditListener(CustomInputEditListener listener){
  this.hostListener = listener;
}

只要您需要打完电话,就只需拨打电话

and whenever you need to call finish just call

hostListener.onFinish();

这篇关于如何正确终止自定义视图类的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆