2017-07-09 35 views
0

我希望用戶輸入一個String字的詞組,如何從EditText獲取單詞?

「動物」是一個空String,我想,當用戶之後或之前進入的話得到它,我想要得到的是單個字在當前位置。

即:

我在我的EditText進入

「貓是動物」字「貓」將被保存在動物的列表,或「狗是動物」和單詞「狗」保存,那麼TextView會說'變成'ok''。

public class MainActivity extends AppCompatActivity { 

ArrayList<String> animal_names = new ArrayList<>(); 
String animal = null; 
TextView say; 
EditText enter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    say = (TextView) findViewById(R.id.textView); 
    enter = (EditText) findViewById(R.id.editText); 


    try { 
     InputStream input = getAssets().open("animals names.txt"); 
     InputStreamReader rd = new InputStreamReader(input); 
     BufferedReader br = new BufferedReader(rd); 
     String line; 
     while ((line = br.readLine()) != null) { 
      animal_names.add(line); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 

public void click(View view) { 
    String s = enter.getText().toString().trim(); 
    if (s.equals(animal+" is an animal")){ 
     say.setText("ok"); 
     animal_names.add(animal); 
    }else if (s.equals("is " + animal + "an animal")){ 
     if (animal_names.contains(animal)){ 
      say.setText("yes"); 
     }else { 
      say.setText("no"); 
     } 
    } 
} 
+1

您目前遇到的問題是什麼? –

+0

不工作,我不知道爲什麼?! –

+0

這不會幫助任何人回答你的問題....請更具描述性 –

回答

0

無法瞭解什麼時候要執行該操作。無論如何,我從你的問題中瞭解到,你需要將TextWatcher() listner添加到你的EditText並比較你的String在其afterTextChanged()方法中。它的樣子:

enter.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       String s1 = s.toString().trim(); 
       if (s1.equals(animal+" is an animal")){ 
        say.setText("ok"); 
        animal_names.add(animal); 
       }else if (s1.equals("is " + animal + "an animal")){ 
        if (animal_names.contains(animal)){ 
         say.setText("yes"); 
        }else { 
         say.setText("no"); 
        } 
       } 
      } 
     }); 

---------- -----------------更新

嘗試這裏面afterTextChanged()或點擊任何其他Button,如果您需要,也可以添加其他條件。

if (s.endsWith("is an animal")) {// for "XXX is an animal" condition 
      animal_names.add(s.substring(0, s.indexOf("is")).trim()); 
      say.setText("yes"); 
    }else if (s.contains("is")&&s.endsWith("an animal")) {// For "is XXX an animal" condition 
     say.setText("yes"); 
     animal_names.add(s.substring(s.lastIndexOf("is")+2, s.indexOf("an animal")).trim()); 
    }else{ 
     say.setText("no"); 
    } 

希望它有幫助!或者更新你的問題,如果你的要求是別的。

+1

不,「動物」是一個空字符串,我想從用戶得到它,當他進入「是一個字」我想得到單詞,怎麼樣? –

+0

不明白,請更新您的問題(例如,即你想要什麼輸出) –

+0

我更新它,請看看:) –

0

使用文本守望當用戶字符串等於動物,,,拯救那些信裏面文本的新字符串守望

0

目前的字符串null is an animalis nullan animal您的代碼檢查(發現你的間距錯字?)

所以,很難說,如果你真的需要一個TextWatcher,但可能是一個正則表達式來代替。例如,一個簡單的正則表達式可能是

"Is ([A-Za-z ]+) an animal?" 

而且

"([A-Za-z ]+) is an animal" 

然後,你會用if (s.matches(regex)),看看如何讓你的動物

How to extract a substring using regex


你也可以使用substring和的組合提取動物名稱