在片段中显示具有progressdialog的资源

时间:2016-01-05 09:17:32

标签: android android-fragments

我可以用三种不同的方式在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));

哪一个是真正的方式,为什么?

1 个答案:

答案 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”