Difference between getResources(R.string.hello_world) and R.string.hello_world

StackOverflow https://stackoverflow.com/questions/22745108

  •  24-06-2023
  •  | 
  •  

문제

Which is the difference between using:

getResources().getText(R.string.hello_world)

and:

R.string.hello_world

The second way, should return an int object. I've just tried:

Toast.makeText(getApplicationContext(), getResources().getText(R.string.hello_world), Toast.LENGTH_LONG).show();

And:

Toast.makeText(getApplicationContext(), R.string.hello_world, Toast.LENGTH_LONG).show();

And seems to work in both ways.

Thanks for help!

도움이 되었습니까?

해결책

Toast.makeText(getApplicationContext(), R.string.hello_world, Toast.LENGTH_LONG)

calls

Toast.makeText(Context, int, int) and it is "translated" like

public static Toast makeText(Context context, int resId, int duration)  
                                throws Resources.NotFoundException {    
   return makeText(context, context.getResources().getText(resId), duration); 
}

all in all it is equal to your first makeText

다른 팁

getResources().getText(R.string.hello_world) : will return String.. And

R.string.hello_world: will return integer (reference location of objects).

And makeToast() method is available for both parameters.

If you passed it string it treat is message.

if you passed it any integer it will treat it as a reference location of String and control will find that String. If No string will available with provided integer then it will throw exception. (ResourceNotFoundException)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top