2013-04-08 109 views
2

我想要使用文本字段(AutoCompleteTextView)從單擊事件的字符串數組中隨機獲取文本值。如何從字符串數組中隨機獲取文本?

的java文件的位置:

String[] questionsOpt = { "I just ejaculated blood", "I just eat", "I just emptied my 401k", "I just exist", 
     "tattooed my face", "threw up yellow stuff", "threw up in my mouth", "took a huge dump"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.start_game); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, questionsOpt); 

    AutoCompleteTextView actvDev = (AutoCompleteTextView) findViewById(R.id.actvDev); 
    actvDev.setThreshold(1); 
    actvDev.setAdapter(adapter); 
} 

如何在這裏循環增加對從按鈕單擊事件的字符串數組隨機找到字符串值?

回答

9

您可以使用Random

Random random = new Random(); // or create a static random field... 
String randString = questionsOpt[random.nextInt(questionsOpt.length)]; 
+0

TNX求助本雅明Sharet – 2013-04-08 10:10:04

+0

發送代碼示例breifly,即不能正常工作 – 2013-04-09 09:56:53

相關問題