Live Editing of Users Input

Live editing of users input

By tagging android, I think you are discussing about android editText, is so you can do it by listening the TextChangedListener,

EDITED: for backspace

editText.addTextChangedListener(new TextWatcher() {
int len=0;
@Override
public void afterTextChanged(Editable s) {
String str = editText.getText().toString();
if(str.length()==4&& len <str.length()){//len check for backspace
editText.append("-");
}
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

String str = editText.getText().toString();
len = str.length();
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

});

Real time updating of values on a form

If you want to display 'realtime' (meaningly, as the user types) values, you can use the keyup values :

  • http://www.w3schools.com/jsref/event_onkeyup.asp
    (for pure javascript)
  • http://api.jquery.com/keyup/ (for
    jquery)

android edittext onchange listener

First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.

EDIT:

To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:

public class YourClass extends Activity {

private EditText yourEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);

yourEditText.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {

// you can call or do what you want with your EditText here

// yourEditText...
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}

Note that the above sample might have some errors but I just wanted to show you an example.

How do I add the ability to edit text within a react component?

So, first of all, I would encourage you not to switch between input fields and divs but rather to use a contenteditable div. Then you just use the onInput attribute to call a setState function, like this:

function Tasks ({item}) {
return(
<div className = 'tasks-container'>
<div contenteditable="true" onInput={e => editTask(item.id, e.currentTarget.textContent)} >
{item.chore}
</div>
</div>
)
}

Then, in the parent component, you can define editTask to be a function that find an item by its id and replaces it with the new content (in a copy of the original tasks array, not the original array itself.

Additionally, you should avoid renaming the variable between components. (listOfTasks -> jobItems). This adds needless overhead, and you'll inevitably get confused at some point which variable is connected to which. Instead say, <MyComponent jobItems={jobItems} > or if you want to allow for greater abstraction <MyComponent items={jobItems} > and then you can reuse the component for listable items other than jobs.

How to make EditText observe a ViewModel's LiveData and forward user input to the ViewModel without using data binding

I also ran into this problem since I don't like the databinding library. I did as @kAliert said, but in my ViewModel to keep the logic there. I just added a simple catch on the function that receives my text changes events in the ViewModel. It works well.

fun editTextChanged(newText: String) {
if (newText == textLiveData.value) {
return
}
}


Related Topics



Leave a reply



Submit