如何检查是否已从TextView中删除一行或多行?

时间:2013-03-31 18:45:11

标签: java android textview

我正在创建一个ide并使用TextView根据我的EditText的行数显示行号。我有它添加新行,但我如何检查是否已删除一行并减少TextView的行数?

et.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
        lines++;
        lineNumbers.append(lines+"\n");            
    }
    return false;
}

});

1 个答案:

答案 0 :(得分:0)

这完成了这项工作。

private void openFile(final File aFile) {
        String nullChk = et.getText().toString();
        exists = true;

        if (!changed || nullChk.matches("")) {
            try {
                currentFile = aFile;
                et.setText(new Scanner(currentFile).useDelimiter("\\Z").next());
                lineNumbers.setText("");
                lines = 1;

                for (int i = 1; i < et.getLineCount(); i++) {
                    lineNumbers.append(lines + "\n");
                    lines++;
            ...
}

et.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    lines++;
                    lineNumbers.append(lines + "\n");
                }
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_DEL)) {
                    lineNumbers.setText("");
                    lines = 0;

                    for (int i = 1; i < et.getLineCount(); i++) {
                        lines++;
                        lineNumbers.append(lines + "\n");
                    }
                }
                return false;
            }
        });