开发者

imeOptions actionDone with Android 2.3

I have a few EditTexts where I've set the imeOptions to actionDone. When I run my application in the emulator using either Android 2.1 or Android 2.2, the enter key on the virtual keyboard becomes "done."

However, (and I've not tested this in the emulator),开发者_JAVA技巧 when I run my application on my phone, which is running Android 2.3 (straight 2.3, Nexus S), the enter key on the virtual keyboard is still a return button and pressing it enters a newline into the EditText.

How can I make the return key on the virtual keyboard say and behave as "done" in Android 2.3?


I have implemented as below and it works great for me. Try out it might help you out.

  EditText m_etDone = (EditText) findViewById(R.id.am_etDone);  
EditText m_etSearch = (EditText) findViewById(R.id.am_etSearch);   
m_etDone.setOnEditorActionListener(new DoneOnEditorActionListener());   
m_etSearch.setOnEditorActionListener(new DoneOnEditorActionListener());                         
     class DoneOnEditorActionListener implements OnEditorActionListener {    
@Override
public boolean onEditorAction(TextView p_v, int p_actionId, KeyEvent   p_event) {
    if (p_actionId == EditorInfo.IME_ACTION_DONE) 
 {
    InputMethodManager m_imm = (InputMethodManager)p_v.getContext()        .getSystemService(Context.INPUT_METHOD_SERVICE);
    m_imm.hideSoftInputFromWindow(p_v.getWindowToken(), 0);
            return true;
        }
        else if(p_actionId == EditorInfo.IME_ACTION_SEARCH)
        {
            Toast.makeText(getApplicationContext(),"Search Text",Toast.LENGTH_SHORT).show();
            return true;
        }
    return false;
    }
}

Layout file:

<EditText 
    android:id="@+id/am_etDone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionNext"
    android:singleLine="true"
    android:imeActionLabel="Done"/>    
<EditText 
    android:id="@+id/am_etSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter search text"
    android:imeOptions="actionSearch"
    android:singleLine="true"
    android:layout_below="@+id/am_etDone"
    android:imeActionLabel="Search"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜