国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android this與Activity.this的區別

2019-12-12 05:13:57
字體:
來源:轉載
供稿:網友

寫語句的時候有兩種情況:

Toast.makeText(AlarmActivity.this,"鬧鐘取消", Toast.LENGTH_SHORT);  <pre name="code" class="java">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

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了。

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的區別的資料整理,后續繼續補充相關資料謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文昌市| 江山市| 长汀县| 厦门市| 渑池县| 左云县| 广东省| 承德市| 霍林郭勒市| 大化| 区。| 信阳市| 合川市| 平和县| 三原县| 怀仁县| 泰安市| 德江县| 临猗县| 顺义区| 玉田县| 大埔县| 巴南区| 博罗县| 葫芦岛市| 常熟市| 宁城县| 越西县| 安福县| 韶山市| 赞皇县| 云南省| 天镇县| 岱山县| 淮滨县| 乐平市| 建瓯市| 大宁县| 南溪县| 周口市| 大化|