How can I convert the android resources int to a string. eg.: android.R.string.cancel?

前端 未结 3 716
后悔当初
后悔当初 2020-12-30 21:11

How can I obtain the string value \"cancel\" from this resource int: android.R.string.cancel ?

thank you

3条回答
  •  甜味超标
    2020-12-30 21:29

    Simply use Context#getString():

    String string = getString(android.R.string.cancel);
    

    I've already tried this approach but with no success... I've a class: public class MyDialogFragment extends DialogFragment {

    A DialogFragment is not a subclass of Context, so you need to get access to a valid one (like your Activity's). Use this:

    String string = getActivity().getString(android.R.string.cancel);
    

    Or as your discovered you can use the Activity passed in onAttach(), but understand you can do this anywhere inside a Fragment as long as you have a valid Context to work with.

提交回复
热议问题