所以我開始使用Android/Java編程。我想嘗試在一個句子中找到一個單詞。假設用戶在EditText中輸入了一句話,即「我的朋友是牛仔」。該應用程序應該從String數組中檢測到牛仔這個詞。即在字符串中取一個字Android/Java
String[] words = {"cowboy", "animal", "monster"};
這是我已經試過:
String[] words = {"cowboy", "animal", "monster"};
Boolean b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById (R.id.editText1);
view = (TextView) findViewById (R.id.textView1);
ok = (Button) findViewById (R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String string = text.getText().toString();
b = string.indexOf("cowboy") > 0;
view.setText(b.toString());
}
});
}
但它給我一個錯誤答案。我的代碼有什麼問題?在indexOf方法中,我不知道如何爲此字符串數組。我希望有人能幫我解決這個問題。謝謝。
哇這個作品哥們!謝謝。 – Dunkey
不客氣。 :) – fida1989