在自定义视图中将 finish() 添加到 ImageButton 单击

add finish() to ImageButton click in custom view

我使用自定义视图在每个布局的底部插入一个通用布局,并以编程方式添加 setonclicklistener。但我想将 finish() 添加到 intent 中,以便在打开新页面时上一页完成。如何做到这一点?

Custom View

public class MenuView extends RelativeLayout {

    private LayoutInflater inflater;

    public MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.homemenu, this, true);
        
        

        ((ImageButton)this.findViewById(R.id.home)).setOnClickListener(home); 
        ((ImageButton)this.findViewById(R.id.search)).setOnClickListener(search); 
        ((ImageButton)this.findViewById(R.id.locate)).setOnClickListener(locate); 
        ((ImageButton)this.findViewById(R.id.log)).setOnClickListener(log); 
        ((ImageButton)this.findViewById(R.id.event)).setOnClickListener(event); 
        ((ImageButton)this.findViewById(R.id.activities)).setOnClickListener(activities); 
        ((ImageButton)this.findViewById(R.id.profile)).setOnClickListener(profile);  
    }
    
   

    private OnClickListener home = new OnClickListener() {
        public void onClick(View v) {
            //getContext().startActivity(new Intent(getContext(), MainActivity.class));
            
            Intent i = new Intent(getContext(), MainActivity.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    }; 
    
    private OnClickListener search = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), Search.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };
    
    private OnClickListener locate = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), Locate.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };
    
    private OnClickListener log = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), Log.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };
    
    private OnClickListener event = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), CreateEvent.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };
    
    private OnClickListener activities = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), Activities.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };

    private OnClickListener profile = new OnClickListener() {
        public void onClick(View v) {
         Intent i = new Intent(getContext(), Profile.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   getContext().startActivity(i);
        }
    };

    
}

以你的方式

getContext().startActivity(i)

使用方法相同,但需要将 getContext() 转换为 Activity

((Activity)getContext()).finish();

如果您在 Fragment 的任何地方使用它,您需要检查它是否 Activity

if( getContext() instanceof Activity )
    ((Activity)getContext()).finish();