Android之在View中执行Activity的跳转

我们经常会在Activity中用到继承LinearLayout或者其他布局的View,但是又想在View的代码里面去执行

Activity的跳转,直接使用startActivity会报错,那么就要用到getContext()来“Returns the context

the view is running in, through which it can access the current theme, resources, etc.”

示例代码如下:

// 跳转到相应的Activity
Intent intent1 = new Intent();
intent1.setClass(getContext(), LocationSelectActivity.class);
getContext().startActivity(intent1);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。