1

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!

4

2 回答 2

0

我认为你需要\n从下面的行中删除

mET.getText().insert(startCursor, "\n" + String.valueOf(getCurrentCursorLine(mET)+2) + ".) ");

因为从键盘输入已经添加了新行。之后,您再次放置另一条新线。

因此,"\n" +从该行中删除。

于 2013-02-26T05:34:49.400 回答
0

您可能已经找到答案,也可能没有。但我认为问题在于您将“mET”设置为“onKey”,我认为这两者都在执行“onKeyDown”和“onKeyUp”。所以这可能是它执行两次操作的原因,因为您正在按下并释放 enter/d-pad 中心键。

所以你真正需要做的就是在之前添加

...keyCode == KeyEvent.KEYCODE_ENTER || KeyEvent.KEYCODE_DPAD_CENTER...

先加这个...

...event.getAction() == KeyEvent.ACTION_DOWN && ...

然后添加其余部分。

于 2014-03-06T01:43:13.180 回答