Show random string

前端 未结 3 989
我在风中等你
我在风中等你 2021-01-03 10:43

i am trying to display a random string each time a button is pressed from a set of strings defined in strings.xml . this is an example of the strings ID\'s

&         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 10:50

    create string array in res/values/array.xml:

      
          
     
        aalia 
        asin 
        sonakshi 
        kajol 
        madhuri
     
    
    

    then in your activity write the code

    String[] array = context.getResources().getStringArray(R.array.custom_array);
    
            String randomStr = array[new Random().nextInt(array.length)];
    
            text_dialog.setText(""+randomStr);
    

提交回复
热议问题