如何从自定义视图类中正确终止一个活动?

huangapple go评论73阅读模式
英文:

How to properly terminate an activity from a custom view class?

问题

此帖子类似。有一个自定义视图(扩展EditText),必须能够在用户按下END键时调用父活动的finish()方法。

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

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()方法。任何帮助或指导将不胜感激。

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

英文:

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.

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

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!

答案1

得分: 2

如果你知道主机,即自定义视图显示的活动,你可以这样做。

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

Java:

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

将此代码放在try和catch块下。

编辑:创建一个接口,让你的主机活动实现它,并将它作为侦听器传递给你的自定义视图,然后在需要时调用它。

例如:

创建一个接口:

interface CustomInputEditListener{
  public void onFinish();
}

在你的主机活动中实现这个接口:

public class MainActivity extends AppCompatActivity implements CustomInputEditListener {

  // 在onCreate()中调用这个方法
  public void setHostListener(){
     suppressInputEditText.setHostEditListener(this);
  }

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

在你的SuppressInputEditText类中创建一个方法:

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

每当需要调用finish时,只需调用:

hostListener.onFinish();
英文:

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

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.

for ex.

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

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

答案2

得分: 1

Kotlin:

(context as? Activity)?.finish()

Java:

((Activity) context).finish()
英文:

Cast context to Activity then call finish like below

Kotlin

(context as? Activity)?.finish()

Java

((Activity) context).finish()

huangapple
  • 本文由 发表于 2020年8月13日 01:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63381756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定