java - Class.this 是什么意思?

标签 java android android-activity

我确实理解 Class.this 在嵌套类的情况下使用,但我遇到了一个在没有任何嵌套类的情况下使用它的代码。

// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);

// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
   // The code in this method will be executed when the numbers View is clicked on.
   @Override
   public void onClick(View view) {
       Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
       startActivity(numbersIntent);
   }
});

行中:

 Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

为什么使用MainActivity.this以及为什么我们不能使用this

最佳答案

因为这里new View.OnClickListener()是匿名内部类。

关于java - Class.this 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457384/

相关文章:

java - 程序的最后一点我无法开始工作。 (构造函数)

java - echo 和 pipe 在 java 命令行环境中是什么意思?

android - 建议 - 向用户显示文件上传进度?

android - 单击按钮在 android Activity 之间移动

java - 在 RXJava android 中为线程设置名称

java - 为什么我不能通过 Intent 传递数据?

java - 2 个日期时间之间的 jodatime 间隔

java - 在应用程序范围内设置一个变量,以便在 session 之间共享

android - Activity、AppCompatActivity、FragmentActivity和ActionBarActivity : When to Use Which?

android - 从右到左增加 Android 按钮的宽度