我可以用三种不同的方式在progressdialog中显示资源。
pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getResources().getString(R.string.please_wait));
哪一个是真正的方式,为什么?
答案 0 :(得分:4)
没有真实的方式。您只需要Context
:
getResources()
班级时(Activity
当您从内部班级打电话时,在<活动课程中)MyActivity.this.getResources()
getActivity().getResources()
班级时,getContext().getResources()
(甚至Fragment
)
当您通过参数context.getResources()
时,Context
当您从view.getContext().getResources()
Context
时,View
以下两个版本具有“等效Context
”,因为您位于Fragment
,就像我可以通过您的问号标记所假设的那样:
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getResources().getString(R.string.please_wait));
,其中
pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));
你得到了Context
entrie申请。
这里有一些参考:
Difference between getContext() , getApplicationContext() , getBaseContext() and “this”