2013-04-07 164 views
0

我對Android開發很新穎。我試圖在鍵入EditText元素時檢測空格,然後立即向EditText元素添加換行符。基本上,試圖用「輸入」擊鍵代替空格擊鍵。我越來越接近了,因爲我在輸入到EditText元素時已經設法檢測到「空格」,但是在檢測到空格時,我正在將「enter」鍵事件分派到EditText元素中。任何幫助將不勝感激。我覺得我正處在答案的尖端,我只需要在正確的方向稍微推動一下。(Android)向EditText發送「ENTER」鍵事件

public void addInputWordListener(){ 
    inputWord.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      sStr = s.toString(); 
      if(sStr.equals("")){ 
       topView.setTextColor(Color.WHITE); 
       return; 
      } 
      if (sStr.endsWith(" ")){ //space found! 
       token = s.toString().trim(); 
       if (token.equalsIgnoreCase(topView.getText().toString())){ 
        inputWord.dispatchKeyEvent(KeyEvent.KEYCODE_ENTER); //doesnt work?? 
        topView.setText(midView.getText()); 
        midView.setText(botView.getText()); 
        botView.setText(MainMenu.we.nextWord()); 
        MainMenu.user.totalWords++; 
       }else{ 
        topView.setTextColor(Color.RED); 
        MainMenu.user.totalWords++; 
        MainMenu.user.numErrors++; 
       } 
      } 
     } 

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

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

    }); 
} 

回答

1

哇,我應該肯定已經嘗試了一些基本的Java東西,然後纔在Android的API海洋中迷路。 只需要:

inputWord.append("\n"); 
+0

你應該..:D – 2013-06-03 11:34:19