How to make edictxt autofocus after showing Toast?

I have an EditText that reads a 13 digit barcode. I want the virtual keyboard to appear on the screen, and EditText always has focus. The following code allows me to write a barcode and search for a product when I press the enter key, and it works well. But if I type a barcode with less than 13 digits or the entered barcode does not exist in my database, I want to show the user a toast, telling him about it. After displaying Toast, I want EditText to automatically increase focus, allowing the user to simply enter the barcode again. After showing Toast, I tried the requestFocus () method, but that didn't work. The soft keyboard is always displayed, but after the Toast, I cannot enter EditText again unless I touch EditText. How can i do this?

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; } }); 

And here is the 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> 

Thanks in advance.

EDIT: In this regard, I found a problem. This solution:

 return true; 

Now it works ...

+4
source share
2 answers

Put this code right next to this Toast post:

 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()); 

It worked for me. Also you need to remove * procura_codbar.requestFocus (); * at the bottom of the code.

+3
source

I managed to get the focus back to EditText by starting a moment before calling the requestFocus() method:

 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; } }); 
+2
source

All Articles