android - 如何从 TextView 启动 Activity?

标签 android android-activity android-intent textview

我有一个 TextView,我想点击它来启动另一个 Activity 。那可能吗? 我尝试了那个(错误的)代码,因为我真的没有找到解决方案:

TextView tv= (TextView)findViewById(R.id.souple);
        tv.setOnClickListener(new View.OnClickListener{
            public void onClick(View v){
                  Intent intent = new Intent(this, OtherActivity.class);
                  startActivity(intent);
            }
     });

错误:token 语法错误,构造错误

那么有什么想法吗? 非常感谢。

最佳答案

您必须通过 Intent 构造函数中的 this 引用您当前的 Activity 。您应该将其更改为 YourActivity.this 以避免歧义。

关于android - 如何从 TextView 启动 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449644/

相关文章:

android - Youtube 与其他网络共享 - 原生还是 webview?

java - Android - 如何关闭由startForeground启动的服务中的通知?

java - 使用 volley 从服务器加载图像

java - appcompat_v7 和 android-support-v7-appcompat 之间的区别?

java - 机器人 :geo Location using intent

android - java.lang.NoClassDefFoundError : Test Module could not resolve classes of other Feature Module, 单元测试 - Kotlin - Android

android - 电话响铃时调用的Activity叫什么

android系统动画时长

android - 我可以阻止快捷方式 (android.intent.action.CREATE_SHORTCUT) 出现在启动器的添加小部件部分吗?

android - 当谈到前景时杀死 Activity