2010-04-05 22 views
8

我想在Android應用軟鍵盤上放置「Go」按鈕如何在android中的SoftKeyBoard中添加Go按鈕及其功能?

對於搜索和其他相關場景可以任何一個人指導我如何實現這一目標?用例子。

任何幫助將appriciated。

+0

我認爲這是自動的東西... – confiq 2010-04-05 11:00:36

+0

從幾天前看到這個問題:http://stackoverflow.com/questions/2568637/how-to-disable-next-button-on-a-edittext-software -keyboard-replace-done-done – 2010-04-05 11:07:01

回答

24

最後我用...

EditText SearchEditText =(EditText)findViewById(R.id.txtMapSearch); 
SearchEditText.setOnEditorActionListener(new OnEditorActionListener(){ 

    @Override 
    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { 
     if(arg1 == EditorInfo.IME_ACTION_SEARCH) 
     { 
      // search pressed and perform your functionality. 
     } 
     return false; 
    } 

}); 
+1

只需要添加一個點,如果您的鍵盤在右下角顯示DONE按鈕,請改用EditorInfo.IME_ACTION_DONE。 – Rorchackh 2012-04-01 22:38:57

+0

指向文檔的鏈接:http://developer.android.com/training/keyboard-input/style.html#Action – 2013-12-11 20:42:38

14

如果您的問題是,你有一個EditText或可編輯的TextView,並且要在softkeyboard進行訴訟的權利按鈕,改爲「進入」,那麼這個屬性添加到您的EditText/TextView的

android:imeActionLabel="actionGo" 

注意它也必須是單行TextView,否則操作按鈕將成爲回車選擇符(箭頭)。

android:singleLine="true" 
+0

實際上我想執行的功能就像在這個軟鍵盤按鈕上按活動的搜索按鈕。 – UMAR 2010-04-05 12:54:34

+0

@Jim Blackler如何在用戶點擊鍵盤上的Action按鈕時進行操作? – Enve 2013-08-13 19:43:10

1

我做這一樣的 「發送」:

使用這個類在你的佈局:

公共類ActionEditText擴展的EditText (上下文); { public ActionEditText(Context context) super(context); }

public ActionEditText(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
} 

public ActionEditText(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
} 

@Override 
public InputConnection onCreateInputConnection(EditorInfo outAttrs) 
{ 
    InputConnection conn = super.onCreateInputConnection(outAttrs); 
    outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; 
    return conn; 
} 

}

在XML:

<com.test.custom.ActionEditText 
      android:id="@+id/postED" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:gravity="top|left" 
      android:hint="@string/msg_type_message_here" 
      android:imeOptions="actionSend" 
      android:inputType="textMultiLine" 
      android:maxLines="5" 
      android:padding="5dip" 
      android:scrollbarAlwaysDrawVerticalTrack="true" 
      android:textColor="@color/white" 
      android:textSize="20sp" /> 
1

我用

android:imeOptions="actionGo" 

和處理去行動我是個使用

etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_DONE) { 
        //your functionality 

        // hide virtual keyboard 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(etSearch.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); 

        return true; 
       } 
       return false; 
      } 
     }); 
相關問題