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

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

  •  24-06-2023
  •  | 
  •  

Pergunta

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!

Foi útil?

Solução

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

Outras dicas

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top