I want to get the after entered text.I have done using TextWatcher.
There are some issues:
For example I want to enter 32.5. In that method I want to add to SET<Product>.
Here each & every number its saving object.That means after enter 3 its add Product object into SET, then add 2 then also adding...
I want to avoid. Once I finish enter EditText,Then want to take it:
final EditText txtQty = new EditText(this);
txtQty.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.v("TAG", "afterTextChanged" + s.toString());
String enterdPrice = txtPrice.getText().toString();
double remainQty =0.00;
Product enterdProduct = new Product();
try{
String enteredQty = s.toString();
enterdProduct.setProductCode(txtCode.getText().toString());
enterdProduct.setPrice(Double.parseDouble(enterdPrice));
//enterdProduct.setQty(Double.parseDouble(enteredQty));
// TO-DO
if (productSet.contains(enterdProduct)) {
productSet.remove(enterdProduct);
}
productSet.add(enterdProduct);
System.out.println("SIZE --" + productSet.size());
}
catch (Exception e) {
e.printStackTrace();
}
});
Please give me idea, How we can get the EditText once enter I enetred text?
You can get entered text on pressing "Enter" button on keyboard.
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
Code from Android tutorial
Can't you use a separate button to add an object using the value in EditText?
Extend the EditText class and override onEndBatchEdit to implement the saving functionality after it has been edited in a 'batch' (could implement some sort of listener interface).
Related
I'm making a shopping cart app that dynamically generates rows as items get added. For each item row, an EditText field is used for the item quantity so that quantities can be modified. Since the quantities need to be integers for calculations, I'm using a parseInt to convert the text grabbed from an altered EditText and trapping out any NFEs in case the input was bad.
When the input is good, everything works fine. When the input is bad, the EditText is set back to its prior value (as desired) but if there is another (dynamically-generated) row below it, the focus shifts to the EditText in that lower row. The desired behavior is for the focus to remain in the EditText that the user wants to change.
Regarding what happens to focus on a successful change -- the virtual keyboard closes on success and nothing keeps focus. In that try/catch block I try to hide the keyboard, but that doesn't help ... it stays open on shifting to the lower field.
Here's an image if it helps to visualize:
before and after hitting ENTER
I've read other posts about using requestFocus() and I've used it here but it doesn't seem to have any effect -- maybe due to all this being generated on the fly? I've also seen posts saying that dynamically generated views don't have any IDs you can grab for being a bit more specific in identifying what should get the focus, but perhaps I'm not understanding it in this context.
So, how do I keep the focus in the field the user wants to edit after handling the NFE and resetting the value of that field?
Here's the specific code that catches that NFE:
try {
quantity = Integer.parseInt(quantityCell.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(getApplicationContext(), "Please enter a valid number",
Toast.LENGTH_LONG).show();
imm.hideSoftInputFromWindow(quantityView.getWindowToken(), 0);
quantityCell.setText("" + savedQuantity);
quantityCell.requestFocus();
return false;
}
... and here is all the code that goes into generating that EditText field on the fly:
// create quantity edittext and add to row
final EditText quantityView = new EditText(this);
quantityView.setLayoutParams(cellParams);
quantityView.setSingleLine();
quantityView.setPadding(5, 5, 5, 5);
quantityView.setText("" + theQuantity);
quantityView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// capture old value of quantity before changing it
EditText et = (EditText)v;
savedQuantity = Integer.parseInt(et.getText().toString());
return false;
}
});
quantityView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_ENTER) {
int quantity;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
TableRow theRow = (TableRow) v.getParent();
TextView nameCell = (TextView) theRow.getChildAt(0);
String name = nameCell.getText().toString();
EditText quantityCell = (EditText) theRow.getChildAt(1);
try {
quantity = Integer.parseInt(quantityCell.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(getApplicationContext(), "Please enter a valid number",
Toast.LENGTH_LONG).show();
imm.hideSoftInputFromWindow(quantityView.getWindowToken(), 0);
quantityCell.setText("" + savedQuantity);
quantityCell.requestFocus();
return false;
}
TextView unitCell = (TextView) theRow.getChildAt(2);
double unit = Double.parseDouble(unitCell.getText().toString().substring(1));
TextView totalCell = (TextView) theRow.getChildAt(3);
double total = Double.parseDouble(totalCell.getText().toString().substring(1));
totalPrice -= total;
totalQuantity -= quantity;
cartDB.changeQuantity(name, quantity, unit);
removeAllRows();
writeOrderTable();
imm.hideSoftInputFromWindow(quantityView.getWindowToken(), 0);
return true;
}
return false;
}
});
// set length of edittext line
productRow.addView(quantityView);
EDIT:
Looking at the code in that catch clause, I'm guessing that the line to hide the virtual keyboard wouldn't do much if the desired field retained any focus in the first place, so maybe I should remove it. The reason it's there is that I tried doing that (hiding the virtual keyboard) to just remove any focus at all and that approach did not work. The field below still grabbed focus and the keyboard remained open.
ANOTHER EDIT:
This does not happen with a physical keyboard -- at least when using by computer keyboard with a Genymotion virtual device (can't use a built-in AVD, I have an AMD cpu). When the same simulator is set to use a virtual keyboard, the problem appears.
FINAL EDIT:
I altered the try/catch block to this and it works as desired. Changes are in the catch clause:
try {
quantity = Integer.parseInt(quantityCell.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(getApplicationContext(), "Please enter a valid number",
Toast.LENGTH_LONG).show();
quantity = savedQuantity;
}
Some code later in the larger block above winds up handling resetting the field when the user inputs bad data. I'm guessing the cause of what was happening was me trying to restore the old value with that setText() method in the old code.
I saw a possible answer here: http://imax-live.blogspot.hk/2012/12/prevent-enter-key-on-edittext-as-multi.html
The idea is to override the keyborad input so that the user cannot use the normal function of the "enter" key
Btw, you can set android:inputType="number" in xml so that only numbers can be entered into the edit text field
class MyTextView extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}
I have written a key listener on a edit text in android.
Following is my code:
textview.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter"
// button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on Enter key press
if (textview.getText().toString().length() == 15) {
textvalue = textview.getText().toString();
textview.setText(replacecardformat());
textview.clearFocus();
Log.e(""TAG, "Executed");
return true;
} else {
return false;
}
}
return false;
}
});
However the log statement is executed only once.Is some problem in my return statement.
Two observations:
if you need listener on each text change use view.addTextChangedListener(TextWatcher). Text Watcher has three methods: one fired before, one after, and one on text change. I suppose that this is what you are looking for. More details and tutorial you can find here
is your textview an TextView or EditText? I am asking since only EditText can receive keyboard input. however TextView can have such listener too. Because its text can also change (see documentation here).
I have a EditText that reads a 13 digit barcode. What I want to do is to keep the virtual keyboard shown on the screen and the EditText to always have focus. The following code let me write the barcode and search for a product when enter key is pressed, and it works well. But if I type a barcode with less then 13 digits or the barcode typed does not exist in my database, I want to show the user a Toast, informing him of it. After displaying the Toast, I want the EditText to gain focus again automatically, letting the user just type the barcode again. After showing the Toast, I tried the requestFocus() method, but it didn't work. The soft keyboard is always shown, but after the Toast, I can't type in the EditText again unless I touch on the 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: Messing with this, I found the problem. Heres the solution:
return true;
Now it works...
Put this code right next to that 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());
This worked for me. Also you need to remove the *procura_codbar.requestFocus();* at the bottom of the code.
I've only been able to get the focus back to the EditText by delaying momentarily 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;
}
});
To All.
Right now i am working on App which launches a dialog(containing EditText) on any alphanumeric key pressed from keyboard. Now i want that whatever user type should appear in EditText box, which is working properly the only issue that i have is, i want the char key which was 1st pressed to open dialog in EditText Box. I am missing the 1st char key which is obviously right. But I retrieve that char as string separately.
Now wanted to attach(show) that char key to inputted EditText from user as 1st letter.
I tried this,
{
final EditText search_d_text = (EditText) dialog
.findViewById(R.id.search_text1);
search_d_text.setText(s1);// si is the 1st char key launch dialog(containing edittext).
search_d_text.setInputType(InputType.TYPE_NULL);
search_d_text
.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// Handle enter key
return true;
}
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Handle IME NEXT key
return true;
}
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Handle IME DONE key
return true;
}
return false;
}
});
search_string = search_d_text.getText().toString();
enterd_text = search_d_text.getText();
// collecting input text.
}
What i am getting is eg. when i type MASK, It shows ASKM.
The setText(s1) is always act as last letter.
I want to show the word or whatever user input starting from 1st key.
Thank You.
Found solution ,
Just moved edit text cursor position. By
search_d_text.setSelection(s1.length()); i.e to next position.
I have been working on passing an edit text value to a string so I can upload the input to my database, but after a lot of time working I have figured out how to do this by pressing enter on the keyboard. I also have a button for uploading the string and I want to have only the button involved, so the upload button will be enter also.
Here is my button:
submit.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
postData();
}
});
Here is the key press enter command:
enter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
name = enter.getText().toString();
return true;
}
return false;
}
});
Here is where I am using the converted string called name:
nameValuePairs.add(new BasicNameValuePair("username", name));
So I want to get rid of the need for the user to press enter before pressing the submit button, so pressing upload will take care of the enter key press code. I tried creating a method to simulate this being pressed but it didn't work, and researching didn't show me a possible way to assign a key press to a button on click.
Any suggestions would be appreciated.
Thanks.
you can try this to perform press button
enter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
name = enter.getText().toString();
submit.performclick();
return true;
}
return false;
}
});