I have an Activity that extends TextWatcher to detect changes in certain EditTexts, so it implements:
public void afterTextChanged(Editable s)
My question is: If there are several EditTexts with .addTextChangedListener(this) set, how can I differentiate which one changed given the Editable object in the afterTextChanged procedure?
Another option, with fewer anonymous inner classes, would be to simply inspect the currently focused View. If your application for TextWatcher hinges solely on changes made by the user while typing, then the changes will always occur in the View that has current focus. Calling getCurrentFocus() from anywhere inside of an Activity or Window will return the View the user is focused on. From inside a TextWatcher, this will almost assuredly be the specific EditText instance.
SDK Docs Link
Hope that Helps!
There's one method to implement this without creating a TextWatcher object for every EditText, but I wouldn't use it:
protected void onCreate(Bundle savedInstanceState) {
// initialization...
EditText edit1 = findViewById(R.id.edit1);
edit1.addTextChangedListener(this);
EditText edit2 = findViewById(R.id.edit1);
edit2.addTextChangedListener(this);
}
private static CharSequence makeInitialString(EditText edit) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.setSpan(edit, 0, 0, Spanned.SPAN_MARK_MARK);
return builder;
}
public void afterTextChanged(Editable s) {
EditText[] edits = s.getSpans( 0, s.length(), EditText.class );
if (edits.length != 1) {
// this mustn't happen
}
// here's changed EditText
EditText edit = edits[0];
}
see TextWatcher for more than one EditText basically create your own class to handle the listener with a constructor that defines the edittext you are monitoring and pass the edittext you are assigning.
Related
I'm using Butterknife(8.4.0) to instantiate my views in a fragment that has several EditTexts.
I'm using these EditTexts to set a string to a specific Model attribute. I don't want to create loads of bind methods for each Edittext so on #onTextChanged I pass in all the edit texts. I'm only interested in AfterTextChanged() so I've also passed that in. I then use the editText ids to specify which model attribute should be set.
#BindView(R.id.edit_text_one) EditText textFieldOne;
#BindView(R.id.edit_text_two) EditText textFieldTwo;
#OnTextChanged(value = {R.id.edit_text_one, R.id.edit_text_two}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void setEditTextFields(EditText editText, Editable editable) {
switch (editText.getId()) {
case R.id.edit_text_one:
myModel.setStringOne(editable.toString());
break;
case R.id.edit_text_two:
myModel.setStringTwo(editable.toString());
break;
}
}
However I'm getting a compile error
Error:(117, 10) error: #OnTextChanged methods can have at most 1 parameter(s). (com.skeeno.android.gamecabinet.Fragment.EditorFragment.setEditTextFields)
I've read here that you just pass in the view as the first argument but that doesn't seem to work since AfterTextChanged is only expecting an editable.
Is there a way to do this?
Any help will be greatly appreciated. Thanks.
Passing View with #onTextChanged is not possible currently. I tried too.
However, below code can be used to get the current view,
//Inside a fragment
View view = getActivity().getCurrentFocus();
Hope this helped.
It is a little bit late but if anyone will have this issue, answer can be helpful therefore, here how i make it work. You cannot send two parameter method for AFTER_TEXT_CHANGED therefore; you need to change method void setEditTextFields(EditText editText, Editable editable) to void setEditTextFields(Editable editable). In this case you need to write #OnTextChanged for all EditText you want to listen.
Here is final code should be;
#BindView(R.id.edit_text_one) EditText textFieldOne;
#BindView(R.id.edit_text_two) EditText textFieldTwo;
#OnTextChanged(value =R.id.edit_text_one, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void edit_text_oneChanged(Editable editable) {
myModel.setStringOne(editable.toString());
}
#OnTextChanged(value =R.id.edit_text_two, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void edit_text_twoChanged(Editable editable) {
myModel.setStringTwo(editable.toString());
}
I have made a class that is responsible for monitoring an EditText widget following the Observer pattern. Its sole function is to disable or re-enable auto-correct based on a method call. I am able to successfully achieve this, but the problem is that the new InputType only applies to new text I add to the EditText - old text still retains the red underline to show that it can be auto-corrected.
Is there any way I can force the new InputType to apply to the entire EditText block, and not simply the new content I add? I tried calling editText.invalidate() and editText.refreshDrawableState() in the hope all the text would refresh, but to no avail.
final class SpellCheckerObserver implements EditTextObserver {
public static final int KEY = KeyGenerator.generateUniqueId();
private int defaultInputType;
SpellCheckerObserver(EditTextSubject subject) {
subject.attach(SpellCheckerObserver.KEY, this);
}
#Override
public void activating(EditText editText) {
defaultInputType = editText.getInputType();
editText.setRawInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
#Override
public void deactivating(EditText editText) {
editText.setInputType(defaultInputType);
}
}
I found out the answer whilst looking through the source code for TextView, where I came across the removeSuggestionSpans() method.I wasn't aware that the suggestions were in fact a type of span, (unsurprisingly, the SuggestionSpan)
This meant I was able to remove the red underline with the following code:
SuggestionSpan[] spans = editText.getText().getSpans(
0, editText.length(), SuggestionSpan.class
);
if (spans.length > 0) {
for (SuggestionSpan span : spans) {
editText.getText().removeSpan(span);
}
}
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function:
if the user clicks confirm, his name will be set to R.string.nfoname
which will then be used in another layout through TextView x = setText(R.string.nfoname);
I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or intent.putextra()
Coming to ur source code i see this
public View x = findViewById(R.string.nfoname);
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Why this editText has to be final?
***x=(View) infoname.getText();***
You just use infoname.getText().toString() you will get the string value of the Edittext's current text.
Dude you can do stuff simply.
public View x = findViewById(R.string.nfoname);
This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.
x=(View) infoname.getText();
Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.
Also, as for this...
TextView x = setText(R.string.nfoname);
It would have to be...
TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));
I have a simple app with 2 edittext boxes. When input is typed into the 1st box one set of calculations are performed. If input is put into the other instead, a different set of calc's occurs.
So, if a number is typed into the first box etBox1, and the user leaves the box, the data from the first box is used to calculate a reesult and put it into the second box. If a number is typed into the second box etBox2, the data is used to calculate a value for the first box.
I tried:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1)
final EditText etBox2 = (EditText) findViewById(R.id.etBox2)
etBox1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#override
public void onFocusChange(View v, boolean lostfocus)
{
if (lostFocus == true)
{ //do my calculations....}
This fires when the focus given to etBox1 instead of waiting for the box to loose focus. This crashes the app because the user hasn't had the chance to input a number into the box. Any ideas why this behaves as a "hasFocus" instead of a "lostFocus"? There is no documentation available on lostFocus at Android's site.
In your question, you have this.
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
You are using the same ID when mapping the text boxes. I guess it should look like this:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox2 = (EditText) findViewById(R.id.etBox2);
Not to mention that you should also get a compilation error with your code, as you define etBox1 two times...
(Unless this is only a typo in your question, and your code actually looks different...)
According to http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
onFocusChange is defined as public abstract void onFocusChange (View v, boolean hasFocus), so your boolean called lostFocus is named backwards and so confusing you, I'd recommend to change it to something like hasFocus.
now you should see that for if statement is the wrong way round, you should be checking if == false