android This 与 activity.this
来源:互联网 发布:软件时间校正 编辑:程序博客网 时间:2024/06/06 01:27
Android activity.this 指的是你当前 activity的实例
表示的就是MainActivity这个类对象本来,这种写法一般用在内部类里,因为在外部类中直接可以用关键字this表示本类,而内部类中直接写this的话表示的是内部类本身,想表示外部类的话就得加上外部类的类名.this。
写语句的时候有两种情况:
[java] view plain copy print?
Toast.makeText(AlarmActivity.this,”闹钟取消”, Toast.LENGTH_SHORT);
Toast.makeText(this,”闹钟5秒后启动”, Toast.LENGTH_SHORT);
用英文在google搜what‘s difference between this and Activity.this,终于有了结果(其实自己后面用了Java里ClassName.this和this 之前搜的是Activity.this,所以没有结果,这点自己要灵活的提高自己的搜索能力了)。 在StackOverFlow找到了答案:
http://stackoverflow.com/questions/10102151/whats-the-difference-between-this-and-activity-this
[java] view plain copy print?
Intent intent = new Intent(this, SecondActivity.class);
eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class)
Intent intent = new Intent(FirstActivity.this, SecondActivity)
this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.
Activity.this points to the instance of the Activity you are currently in.
this是你当前对象的引用,在你的例子中你肯定在内部类ClickEvent里面实现intent,他指向的是ClickEvent,而不是你要传入的Activity。
Activity.this指向你所填写的Activity名字的一个实例,也是引用。
其实这是java的基础,我自己忘了。
this作为当前对象,直接用在Activity里面是没问题的,当this在匿名内部类中使用,当前的对象就变成new的内部类,而你传入的东西如果是整个Activity的话,就要Activity.this了。
[java] view plain copy print?
Button b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlarmActivity.this,”闹钟5秒后启动”, Toast.LENGTH_SHORT);
}};
所以在这里面需要指定是哪个activity的,Toast的那条语句移到外面,删掉AlarmActivity也行。
所以还是网友建议,全部用成Activity.this,清晰。
- android This 与 activity.this
- Android this与Activity.this的区别
- Android:Activity.this与getApplicationContext()
- android与java浅谈this与activity.this
- Android问题之this与Activity.this的区别
- android中this、Activity.this、context区别与联系
- Android问题之this与Activity.this的区别
- Android问题之this与Activity.this的区别
- Android中this与Activity.this的区别
- Android问题之this与Activity.this的区别
- android getApplicationContext()与activity.this的区别
- getApplicationContext()与Activity.this
- Activity.this 与this的区别
- getApplicationContext()与Activity.this区别
- andriod context 与 Activity.this与this.getApplicationContext()区别
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- Android编程之getApplicationContext(),activity.this,this的区别
- Android中Context、getApplication、getApplicationContext、this、Activity.this的区别
- 单台linux下配置多个tomcat
- java时间的毫秒数和C#的datetime时间相互转化方法
- Spring 工作原理
- UGUI按钮动态绑定和触发方法
- ARM(A83T )开发环境搭建(二)
- android This 与 activity.this
- 学习笔记之开发相关概念(2)--内网和外网
- wince心得(动态调用动态库)
- Java处理中文乱码
- 利用Alluxio系统提升按需数据分析服务的性能
- Java匿名对象的介绍
- OPENJUDGE 1.8 16:矩阵石头剪刀布
- Java 处理日期年、月、日、周加减运算
- 【LeetCode】508. Most Frequent Subtree Sum【M】【40】