2015-11-16 71 views
-1

我正在嘗試爲editText創建命令,因此當您輸入關鍵字時,一些代碼將運行。我現在正在使用此代碼,但它似乎不起作用。editText'Enter'else if命令

@Override 
    public void editText (String editText) 
    { 
     if(editText == ("closer")) 
     { 
      ((ViewGroup.MarginLayoutParams) red.getLayoutParams()).topMargin += 1; 
//    image.setRotation(image.getRotation() + 1); 
      red.requestLayout(); 
      ((ViewGroup.MarginLayoutParams) blue.getLayoutParams()).topMargin -= 1; 
//    image2.setRotation(image2.getRotation() + 1); 

     } 

當我輸入單詞並按回車時,編輯文本只是創建一個新行。但我希望能夠按Enter鍵和代碼運行,輸入什麼。

+0

怎麼樣'如果(EDITTEXT ==(」更接近「))'? –

+0

[我如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – 323go

回答

1

您可以處理這樣的回車鍵:

editText.setOnKeyListener(new OnKeyListener() { 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     // If the event is a key-down event on the "enter" button 
     if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
      (keyCode == KeyEvent.KEYCODE_ENTER)) { 
      // Perform action on key press 
      if(editText.getText().equals("closer")){ 
       ((ViewGroup.MarginLayoutParams)red.getLayoutParams()) 
       .topMargin += 1; 
       red.requestLayout(); 
       ((ViewGroup.MarginLayoutParams)blue.getLayoutParams()) 
       .topMargin -= 1; 
      } 
      return true; 
     } 
     return false; 
    } 
}); 
0

試試你的EditText的單行屬性設置爲true

android:singleLine="true"