将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();
更多推荐
发布评论