首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android enter键监听器

Android enter键监听器
EN

Stack Overflow用户
提问于 2012-10-17 23:22:21
回答 2查看 22.3K关注 0票数 7

有没有人能帮我弄个软键盘enter键监听器?

我需要一个enter键监听器,比如一个按钮监听器,里面有几个editext监听器,像这样

代码语言:javascript
复制
enterkey.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) { 
        if(editext1.getText().toString().equalsIgnoreCase("test1")) {
            button3.performClick();
        }
        if(editext1.getText().toString().equalsIgnoreCase("test2")) {
            button4.performClick();
        }
    }
);

我还需要知道这样的事情是否正确?

代码语言:javascript
复制
if(editext1.getText().toString().equals.null)) {
       testwrong.setText("Wrong"); 

我现在已经尝试使用此代码,但当我按回车键时,仍然得到一个空值。有没有人能提出一个解决方案来避免这种情况?

代码语言:javascript
复制
editext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            if ("test1".equalsIgnoreCase(anstext.getText().toString())) {
                but4.performClick();
            }
        } else if ("test2".equalsIgnoreCase(editext.getText().toString())) {
            but5.performClick();
        }
        if ("test5".equalsIgnoreCase(editext.getText().toString())) {
            but6.performClick();
        }
        if ("test7".equalsIgnoreCase(editext.getText().toString())) {
            but7.performClick();
        }
        if (editext.getText().toString() != null) {
            testwrong.seText("wrong");
        }
        return true;
    }
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-17 23:29:51

Stack Overflow用户

发布于 2012-10-17 23:32:51

EditText中,您应该使用imeOptions指定键盘操作。

代码语言:javascript
复制
<EditText
       android:id="@+id/query"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:imeOptions="actionGo"
       android:inputType="text" />

在你的练习课上:

代码语言:javascript
复制
 EditText editText = (EditText) findViewById(R.id.query);
 editText.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_GO) {

                        return true;
                    }
                    return false;
                }
            });
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12937731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档