将Finish()添加到ImageButton,单击自定义视图(add finish() to ImageButton click in custom view)

编程入门 行业动态 更新时间:2024-10-23 15:18:54
将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);
        }
    };

    
} 
  
 

I used a custom view to insert a common layout in the bottom of every layout and added setonclicklistener programmatically. But I want to add finish() to intent so that on opening new page the previous page gets finish. How to do this?

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

in the way you are doing

getContext().startActivity(i)

same way use but you need to convert getContext() to Activity

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

If anywhere you are using this from Fragment you need to check whether its Activity or not.

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

更多推荐

本文发布于:2023-08-06 17:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   视图   单击   ImageButton   Finish

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!