如何在显示Toast后进行edittext自动对焦?

时间:2012-08-06 11:48:02

标签: android focus android-edittext autofocus

我有一个读取13位数条形码的EditText。我想要做的是保持屏幕上显示的虚拟键盘和EditText始终具有焦点。下面的代码让我编写条形码并在按下回车键时搜索产品,并且效果很好。但是如果我输入的条形码少于13位数或者我的数据库中不存在条形码类型,我想向用户显示Toast,通知他。显示Toast后,我希望EditText再次自动获得焦点,让用户再次输入条形码。在显示Toast之后,我尝试了requestFocus()方法,但它没有用。始终显示软键盘,但在Toast之后,除非我触摸EditText,否则我无法再次输入EditText。我怎么能这样做?

    final EditText procura_codbar = (EditText)
                findViewById(R.id.procurar_produto_codbar);
    procura_codbar.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                String codbar = procura_codbar.getText().toString();
                if (codbar.length()<13){   
                    Toast.makeText(MainActivity.this,
                           "type a 13 digit barcode",
                           Toast.LENGTH_LONG).show();
                }
                else{
                    if (bdh!=null){
                        bdh.closedb(); bdh.close();
                    }
                    bdh = new DBHelper(MainActivity.this);
                    Log.i("CODBAR", codbar);
                    produto prod_ = bdh.getProduto(codbar);
                    if (prod_!=null){
                        showDialogPreco(prod_);
                        procura_codbar.setText("");
                    }else{
                        Toast.makeText(MainActivity.this,
                                                     "Product not found",
                                                     Toast.LENGTH_SHORT).show();
                        procura_codbar.setSelection(codbar.length());
                    }
                }
                procura_codbar.requestFocus();
                procura_codbar.setSelection(codbar.length());
            }

            return false;
        }
    });

这是XML:

<EditText
    android:id="@+id/procurar_produto_codbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:inputType="textNoSuggestions|number"
    android:textSize="22sp"
    android:maxLength="13"
    android:layout_toRightOf="@+id/tv_procura_codbar" >

    <requestFocus />
</EditText>

提前致谢。

编辑:对此,我发现了问题。下面是解决方案:

return true;

现在它有效......

2 个答案:

答案 0 :(得分:3)

将此代码放在Toast Message旁边:

Toast.makeText(MainActivity.this,
                       "type a 13 digit barcode",
                       Toast.LENGTH_LONG).show();

procura_codbar.setFocusableInTouchMode(true);
procura_codbar.setFocusable(true);
procura_codbar.requestFocus();
procura_codbar.setSelection(codbar.length());

这对我有用。您还需要删除代码底部的 * procura_codbar.requestFocus(); *

答案 1 :(得分:2)

我只能通过在调用EditText方法之前暂时延迟重点回到requestFocus()

final Handler handler = new Handler();
final EditText myEditText = (EditText) findViewById(R.id.editText1);

myEditText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        // See if user presses enter
        if (event.getAction() == KeyEvent.ACTION_DOWN &&
            keyCode == KeyEvent.KEYCODE_ENTER) {

            Toast.makeText(MainActivity.this, "You pressed enter!",
                        Toast.LENGTH_SHORT).show(); 

            // Put focus back in the EditText after brief delay                  
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {                 
                    myEditText.requestFocus();

                    // Select all text
                    myEditText.setSelection(0, myEditText.getText().length());                  
                }                   
            }, 200);

            return true;                    
        }

        return false;
    }       
});