I want to generate sequence number in EditText when I press enter button.
Detail explanation :
Everytime I want to write a character for the first time in edittext should begin with this string --> "1."
And after I finish write the sentence and press enter button (new line), the next sentence should begin with the next sequence number
For example : When I write in edittext for the first time like this
this is the first sentence
, it should be like this
1.) this is the first sentence
And then when I press enter button to write the next sentence like this
this is the second sentences
it should be like this
2.) this is the second sentences
I try to write the code, but there was an unexpected result when I try to write the first character in EditText
This is my whole code :
public class MainActivity extends Activity {
private EditText mET;
public int getCurrentCursorLine(EditText editText)
{
int selectionStart = Selection.getSelectionStart(editText.getText());
Layout layout = editText.getLayout();
if (!(selectionStart == -1)) {
return layout.getLineForOffset(selectionStart);
}
return -1;
}
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
mET.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
int startCursor = 0;
startCursor = mET.getSelectionStart();
if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
mET.getText().insert(startCursor, "\n" + String.valueOf(getCurrentCursorLine(mET)+2) + ".) ");
return true;
}
return false;
}
});
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mET = (EditText) findViewById(R.id.mEditText);
Editable text = mET.getText();
text.insert(mET.getSelectionStart(), "1.) ");
mET.addTextChangedListener(mTextEditorWatcher);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
That's my whole code guys..
By using my code above, the result was become like this :
1.) this is the first sentence
2.)
3.) this is the second sentences
4.)
5.) this is the third sentences
The question is, why it was executed twice?
And, when I pressed enter button to insert a new line in the line 2 for example, the result was become like this :
1.) this is the first sentence
2.) this is the second sentences
3.)
4.)
3.) this is the second sentences
4.)
5.) this is the third sentences
I want to update the whole number when I insert a new line in the random line.
now I need your help to fix it.
Thanks!